594
社区成员
发帖
与我相关
我的任务
分享
declare @t table([代理] varchar(10),[收入] int,[同比] int,[环比] int);
insert into @t select '代理人1',100,200,300 union all
select '代理人2',110,220,330;
--select * from @t;
select CASE 名称 WHEN '收入'THEN [代理]+' 当期数据' ELSE 名称 END AS 名称,收入x AS 收入 from
(select [代理],[收入],[同比] ,[环比] from @t) pvt
unpivot (收入x FOR 名称 in ([收入],[同比],[环比])) unpvt
/*
名称 收入
--------------------- -----------
代理人1 当期数据 100
同比 200
环比 300
代理人2 当期数据 110
同比 220
环比 330
*/
- -!总算有了一盘unpivot了
declare @t table([代理] varchar(10),[收入] int,[同比] int,[环比] int);
insert into @t select '代理人1',100,200,300 union all
select '代理人2',110,220,330;
--select * from @t;
select CASE 名称 WHEN '收入'THEN [代理]+' 当期数据' ELSE 名称 END AS 名称,收入x AS 收入 from
(select [代理],[收入],[同比] ,[环比] from @t) pvt
unpivot (收入x FOR 名称 in ([收入],[同比],[环比])) unpvt
/*
名称 收入
--------------------- -----------
代理人1 当期数据 100
同比 200
环比 300
代理人2 当期数据 110
同比 220
环比 330
*/
- -!总算有了一盘unpivot了
select 代理人,'当期数据' as 类型,(select 收入 from tb where 代理人 = a.代理人) as 金额
from tb a
union all
select 代理人,'同比',(select 同比 from tb where 代理人 = a.代理人)
from tb a
union all
select 代理人,'环比',(select 环比 from tb where 代理人 = a.代理人)
from tb a
order by 代理人