SQL server中转置函数(列转行PIVOT()函数、行转列UNPIVOT()函数)

首先,通过两个表格说明本次要讲解的为什么要使用这两个函数?

图一:

时间机构号销售渠道保费A保费B保费C保费D
statdatebranch_codemgrlonginsAlonginsBlonginsClonginsD
201902011100001100100100100
201902011100002100100100100
201902011100003100100100100

图二:

时间机构号保费项目1个险渠道2银保渠道3团险渠道
20190201110000保费A100100100
20190201110000保费B100100100
20190201110000保费C100100100
20190201110000保费D100100

            100

客户的需求,就是希望行标题和列标题互换:保费项目和销售渠道对角线转置。

一、创建测试表:

create table  test_hangzhuanlie(
statdate varchar(8),--统计日期
branch_code varchar(6),--机构编号
mgr varchar(2),--销售渠道
longinsA [decimal](20, 2),--保费A
longinsB [decimal](20, 2),--保费B
longinsC [decimal](20, 2),--保费C
longinsD [decimal](20, 2),--保费D
stamp datetime--时间戳
)

二、增加测试数据:

--select * from test_hangzhuanlie
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '110000', '1', '100', '100', '100', '100', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '120000', '1', '200', '200', '200', '200', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '130000', '1', '300', '300', '300', '300', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '140000', '1', '400', '400', '400', '400', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '150000', '1', '500', '500', '500', '500', GETDATE())

insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '110000', '2', '100', '100', '100', '100', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '120000', '2', '200', '200', '200', '200', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '130000', '2', '300', '300', '300', '300', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '140000', '2', '400', '400', '400', '400', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '150000', '2', '500', '500', '500', '500', GETDATE())

insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '110000', '3', '100', '100', '100', '100', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '120000', '3', '200', '200', '200', '200', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '130000', '3', '300', '300', '300', '300', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '140000', '3', '400', '400', '400', '400', GETDATE())
insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp) 
values ('20190201', '150000', '3', '500', '500', '500', '500', GETDATE())

三、通过两个函数求得所要求的数据展示表格:

select statdate, branch_code, name, [1] as '1个险渠道', [2] as '2银保渠道', [3] as '3团险渠道'
from
(
select * from 
(
select statdate, branch_code, mgr, target, 
	case 
		when [column] = 'longinsA' then '保费A'
		when [column] = 'longinsB' then '保费B'
		when [column] = 'longinsC' then '保费C'
		when [column] = 'longinsD' then '保费D'
	end as name
from 
(		
select statdate, branch_code, mgr, 
SUM(longinsA) as longinsA, SUM(longinsB) as longinsB, SUM(longinsC) as longinsC, SUM(longinsD) as longinsD
from test_hangzhuanlie a group by statdate, branch_code, mgr
) a
unpivot (target FOR [column] IN ([longinsA], [longinsB], [longinsC], [longinsD])) AS t
) b
pivot(max(target) for mgr in ([1],[2],[3])) p
) x

总结:通过两个函数就可以得到上面要求的表格格式。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值