Ⅰ sql 查詢出一列內容,如何讓它成一行顯示。
進行行轉列下面是相關語法等
PIVOT 用於將列值旋轉為列名(即行轉列),在 SQL Server 2000可以用聚合函數配合CASE語句實現
PIVOT 的一般語法是:PIVOT(聚合函數(列) FOR 列 in (…) )AS P
注意:PIVOT、UNPIVOT是SQL Server 2005 的語法,使用需修改資料庫兼容級別(在資料庫屬性->選項->兼容級別改為 90 )
SQL2008 中可以直接使用
完整語法:
table_source
PIVOT(
聚合函數(value_column)
FOR pivot_column
IN(<column_list>)
)
View Code
UNPIVOT 用於將列明轉為列值(即列轉行),在SQL Server 2000可以用UNION來實現
完整語法:
table_source
UNPIVOT(
value_column
FOR pivot_column
IN(<column_list>)
)
Ⅱ SQL語句 列轉行
-- ========================= PIVOT 行列轉置 ===========================
-- 1、【行列轉置PIVOT】
declare @Score table(StuNo varchar(10), StuName varchar(50), CourseName varchar(50), Score int)
insert into @Score
select '1', 'Tom', 'Math', 80 union all
select '1', 'Tom', 'English', 82 union all
select '1', 'Tom', 'Geography', 84 union all
select '2', 'Jone', 'Math', 79 union all
select '2', 'Jone', 'English', 88 union all
select '2', 'Jone', 'Geography',86
select * from @Score
SELECT StuNo, StuName, Math, English, [Geography]
FROM @Score PIVOT (MAX(Score) FOR CourseName in (Math, English, [Geography]) ) AS ScoreList
ORDER BY StuNo
-- 2、【列行轉置UNPIVOT】
declare @ScoreList table(StuNo varchar(10), StuName varchar(50), Math int, English int, [Geography] int)
insert into @ScoreList
select '1', 'Tom', 80, 82, 84 union all
select '2', 'Jone', 79, 88, 86
select * from @ScoreList
SELECT StuNo, StuName, CourseName, Score
FROM @ScoreList UNPIVOT (Score FOR CourseName in (Math, English, [Geography]) ) AS ScorePvtTable
ORDER BY StuNo
Ⅲ SQL 查詢 表格的轉換,將列轉換成行顯示
SELECTSN,SUM(CASEWHENField_Name='BL1_Ver'THENField_ValueEND)BL1_Ver,
SUM(CASEWHENField_Name='BL2'THENField_ValueEND)BL2,
SUM(CASEWHENField_Name='BL3'THENField_ValueEND)BL3
FROM表名
GROUPBYSN
Ⅳ sql 最簡單的列轉行
oracle中列傳行可用wm_concat來實現。
如test表中數據如下:
現要將name列一列顯示成行,可用如下語句:
select wm_concat(name) from test;結果:
Ⅳ SQL表列數據轉成行數據
create table #A03(AId varchar(30),
CardNo varchar(30),SumMoney money,Type varchar(30)) --臨時表
create table #A07(AId varchar(30),
CardNo varchar(30),SumMoney money,Type varchar(30))
insert into #A03 select AId,CardNo,SumMoney,Type from A
where AID='B6BA' and CardNo='996000010003'
insert into #A70 select AId,CardNo,SumMoney,Type from A
where AID='B6BA' and CardNo='996000010070'
select a.AID,a.CardNo,a.SumMoney,a.Type,
b.CardNo,b.SumMoney,b.Type
from #A03 a right join #A70 b on a.CardNo='996000010003' and b.CardNo='996000010070'
--你試試
Ⅵ sql語句怎麼把列變成行
create table rotatetable1 (序號 int,company char(66),box_weight char(12),廢塑料numeric(10,2)),廢五金 numeric(10,2)),廢鋼鐵 numeric(10,2)),廢紙 numeric(10,2)),廢有色 numeric(10,2)),廢纖維 numeric(10,2)),其它 numeric(10,2)),合計 numeric(10,2)));
insert into rotatetable1(company,box_weight) select name ,'weight' from sum1 group by name;
insert into rotatetable1(company,box_weight) select name ,'box' from sum1 group by name;
update rotatetable1 set 廢塑料=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='廢塑料';
update rotatetable1 set 廢塑料=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='廢塑料';
::: :::
update rotatetable1 set 其它=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='其它';
update rotatetable1 set 其它=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='其它';
::: :::
update rotatetable1 set 合計=廢塑料+廢五金+廢鋼鐵+廢紙+廢有色+廢纖維+其它;
(所有涉及表的行列轉換均可按照這種方式實現。)