case table pivot / pivot 多列 - MSSQL 2008
case table pivot / pivot multiple column - MSSQL 2008
我有以下 sql 数据结构,需要将其从每行一个月转换为每行一列。
我知道 pivot 只能用一个 columbn 来完成,但想不出一个合适的方法来使用 case 语句来翻转 table。
我想改变这个:
对此:
SQL创建的结构是:
create table records (month int,apples int, grapes int, oranges int);
insert into records values (1,1,43,12)
insert into records values (2,23,43,5)
insert into records values (3,32,43,12)
insert into records values (4,23,43,12)
insert into records values (5,23,434,12)
insert into records values (6,23,43,12)
insert into records values (7,33,43,12)
insert into records values (8,23,55,12)
insert into records values (9,23,4332,12)
insert into records values (10,223,43,18)
insert into records values (11,223,43,12)
insert into records values (12,23,143,122)
我不确定如何在一条语句中或通过给服务器增加额外开销来做到这一点?
可以使用 case 语句正确处理,但需要连接语句:
select type,sum(jan)jan,sum(feb)feb,sum(mar) mar from ( select
'apples' type,case when month=1 then apples else 0 end jan
,case when month=2 then apples else 0 end feb
,case when month=3 then apples else 0 end mar
from #records ) t group by type
UNION
select type,sum(jan)jan,sum(feb)feb,sum(mar) mar from ( select
'oranges' type,case when month=1 then oranges else 0 end jan
,case when month=2 then oranges else 0 end feb
,case when month=3 then oranges else 0 end mar
from #records ) t group by type
我确定这不是他的最佳方法,欢迎提出建议。
一如既往的提前致谢
它需要 unpivot
和 pivot
的组合。试试这个方法
SELECT *
FROM (SELECT month,
names,
value
FROM Yourtable
CROSS apply (VALUES ('apples',apples),('grapes',grapes),('oranges',oranges)) tc (names, value)) a
PIVOT (Max(value)
FOR month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) pv
Live Demo
我有以下 sql 数据结构,需要将其从每行一个月转换为每行一列。
我知道 pivot 只能用一个 columbn 来完成,但想不出一个合适的方法来使用 case 语句来翻转 table。
我想改变这个:
对此:
SQL创建的结构是:
create table records (month int,apples int, grapes int, oranges int);
insert into records values (1,1,43,12)
insert into records values (2,23,43,5)
insert into records values (3,32,43,12)
insert into records values (4,23,43,12)
insert into records values (5,23,434,12)
insert into records values (6,23,43,12)
insert into records values (7,33,43,12)
insert into records values (8,23,55,12)
insert into records values (9,23,4332,12)
insert into records values (10,223,43,18)
insert into records values (11,223,43,12)
insert into records values (12,23,143,122)
我不确定如何在一条语句中或通过给服务器增加额外开销来做到这一点?
可以使用 case 语句正确处理,但需要连接语句:
select type,sum(jan)jan,sum(feb)feb,sum(mar) mar from ( select
'apples' type,case when month=1 then apples else 0 end jan
,case when month=2 then apples else 0 end feb
,case when month=3 then apples else 0 end mar
from #records ) t group by type
UNION
select type,sum(jan)jan,sum(feb)feb,sum(mar) mar from ( select
'oranges' type,case when month=1 then oranges else 0 end jan
,case when month=2 then oranges else 0 end feb
,case when month=3 then oranges else 0 end mar
from #records ) t group by type
我确定这不是他的最佳方法,欢迎提出建议。
一如既往的提前致谢
它需要 unpivot
和 pivot
的组合。试试这个方法
SELECT *
FROM (SELECT month,
names,
value
FROM Yourtable
CROSS apply (VALUES ('apples',apples),('grapes',grapes),('oranges',oranges)) tc (names, value)) a
PIVOT (Max(value)
FOR month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) pv