当前位置:   article > 正文

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

unpivot函数sql

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

图一:

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

图二:

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

            100

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

一、创建测试表:

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

二、增加测试数据:

  1. --select * from test_hangzhuanlie
  2. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  3. values ('20190201', '110000', '1', '100', '100', '100', '100', GETDATE())
  4. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  5. values ('20190201', '120000', '1', '200', '200', '200', '200', GETDATE())
  6. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  7. values ('20190201', '130000', '1', '300', '300', '300', '300', GETDATE())
  8. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  9. values ('20190201', '140000', '1', '400', '400', '400', '400', GETDATE())
  10. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  11. values ('20190201', '150000', '1', '500', '500', '500', '500', GETDATE())
  12. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  13. values ('20190201', '110000', '2', '100', '100', '100', '100', GETDATE())
  14. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  15. values ('20190201', '120000', '2', '200', '200', '200', '200', GETDATE())
  16. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  17. values ('20190201', '130000', '2', '300', '300', '300', '300', GETDATE())
  18. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  19. values ('20190201', '140000', '2', '400', '400', '400', '400', GETDATE())
  20. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  21. values ('20190201', '150000', '2', '500', '500', '500', '500', GETDATE())
  22. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  23. values ('20190201', '110000', '3', '100', '100', '100', '100', GETDATE())
  24. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  25. values ('20190201', '120000', '3', '200', '200', '200', '200', GETDATE())
  26. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  27. values ('20190201', '130000', '3', '300', '300', '300', '300', GETDATE())
  28. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  29. values ('20190201', '140000', '3', '400', '400', '400', '400', GETDATE())
  30. insert into test_hangzhuanlie (statdate, branch_code, mgr, longinsA, longinsB , longinsC, longinsD, stamp)
  31. values ('20190201', '150000', '3', '500', '500', '500', '500', GETDATE())

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

  1. select statdate, branch_code, name, [1] as '1个险渠道', [2] as '2银保渠道', [3] as '3团险渠道'
  2. from
  3. (
  4. select * from
  5. (
  6. select statdate, branch_code, mgr, target,
  7. case
  8. when [column] = 'longinsA' then '保费A'
  9. when [column] = 'longinsB' then '保费B'
  10. when [column] = 'longinsC' then '保费C'
  11. when [column] = 'longinsD' then '保费D'
  12. end as name
  13. from
  14. (
  15. select statdate, branch_code, mgr,
  16. SUM(longinsA) as longinsA, SUM(longinsB) as longinsB, SUM(longinsC) as longinsC, SUM(longinsD) as longinsD
  17. from test_hangzhuanlie a group by statdate, branch_code, mgr
  18. ) a
  19. unpivot (target FOR [column] IN ([longinsA], [longinsB], [longinsC], [longinsD])) AS t
  20. ) b
  21. pivot(max(target) for mgr in ([1],[2],[3])) p
  22. ) x

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/821557
推荐阅读
相关标签
  

闽ICP备14008679号