SQL2008 R2 SP3中使用pivot实现行转列

同事遇到一个问题,怎么实现行转列,参考了别人的示例,我是这样解决的。

创建数据源:

 1 create table #temp (
 2     objectid int,
 3     sitename nvarchar(32),
 4     cities nvarchar(32),
 5     projectdurationno nvarchar(32),
 6     fullname nvarchar(32),
 7     createdate datetime,
 8     nodename nvarchar(32)
 9 )
10 
11 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'1','海口市','','','10 30 2015 10:26AM','开始')
12 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'1','海口市','','','10 30 2015 10:26AM','规划库')
13 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'1','海口市','','','10 30 2015 10:27AM','投资')
14 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'1','海口市','','','10 30 2015  4:37PM','站点')
15 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'1','海口市','','','10 30 2015  4:37PM','方案')
16 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'1','海口市','','','10 30 2015  4:38PM','集成')
17 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'1','海口市','','','10 30 2015  6:03PM','开始')
18 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'1','海口市','','','10 30 2015  6:03PM','规划库')
19 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'1','海口市','','','10 30 2015  6:03PM','投资')
20 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'1','海口市','','','10 30 2015  9:31PM','站点')
21 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'1','海口市','','','10 30 2015  9:31PM','方案')
22 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'1','海口市','','','10 30 2015  9:32PM','集成')
23 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'1','海口市','','','10 30 2015  9:32PM','光缆厂家')
24 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:13PM','开始')
25 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:13PM','规划库')
26 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:13PM','投资')
27 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:14PM','站点')
28 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:14PM','方案')
29 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:18PM','集成')
30 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:18PM','光缆厂家')
31 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:19PM','分布系统、设备安装')
32 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:19PM','光缆到位')
33 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:24PM','PTN上线')
34 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:25PM','开通')
35 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'1','海口市','','','11  3 2015  3:26PM','结束')
36 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'1','海口市','','','11  3 2015  3:45PM','开始')
37 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'1','海口市','','','11  3 2015  3:45PM','规划库')
38 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'1','海口市','','','11  3 2015  3:45PM','投资')
39 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'1','海口市','','','11  3 2015  3:46PM','站点')
40 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'1','海口市','','','11  3 2015  3:46PM','方案')
41 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(9,'1','海口市','','','11  3 2015  4:26PM','开始')
42 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(9,'1','海口市','','','11  3 2015  4:26PM','规划库')
43 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(9,'1','海口市','','','11  3 2015  4:26PM','投资')
44 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(10,'1','海口市','','','11  3 2015  4:29PM','开始')
45 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(10,'1','海口市','','','11  3 2015  4:29PM','规划库')
46 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(10,'1','海口市','','','11  3 2015  4:29PM','投资')
47 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:33PM','开始')
48 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:33PM','规划库')
49 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:33PM','投资')
50 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:33PM','站点')
51 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:41PM','方案')
52 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:42PM','集成')
53 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:42PM','光缆厂家')
54 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:42PM','分布系统、设备安装')
55 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:43PM','光缆到位')
56 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:44PM','PTN上线')
57 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:44PM','开通')
58 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'1','海口市','','','11  3 2015  4:46PM','结束')
59 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'2','海口市','','','11  3 2015  3:30PM','开始')
60 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'2','海口市','','','11  3 2015  3:30PM','规划库')
61 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'2','海口市','','','11  3 2015  3:30PM','投资')
62 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'2','海口市','','','11  3 2015  3:31PM','站点')
63 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'2','海口市','','','11  3 2015  3:31PM','方案')
64 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'2','海口市','','','11  3 2015  3:32PM','集成')
65 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'2','海口市','','','11  3 2015  3:32PM','光缆厂家')
66 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(5,'2','海口市','','','11  3 2015 10:31AM','开始')
67 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(5,'2','海口市','','','11  3 2015 10:31AM','规划库')
68 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(5,'2','海口市','','','11  3 2015 10:34AM','投资')
69 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'7','海口市','','','10 30 2015  5:36PM','开始')
70 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'7','海口市','','','10 30 2015  5:36PM','规划库')
71 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'7','海口市','','','10 30 2015  5:36PM','投资')
72 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'7','海口市','','','10 30 2015  5:37PM','站点')
73 insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'7','海口市','','','10 30 2015  5:37PM','方案')

 

方案一、使用pivot进行行转列:

 1 select objectid,SiteName,[开始],[规划库],[投资],[站点],[方案],[集成]
 2     from(
 3         select objectid,sitename,NODE_NAME,createdate from #temp
 4     ) as t10 
 5     pivot(
 6         max(createdate) 
 7         for node_name in 
 8         ([开始],[规划库],[投资],[站点],[方案],[集成])
 9     ) as t11
10     order by sitename,objectid

 方案二、group by 方案

 1     
 2 select t10.sitename,t10.objectid,
 3 (case t10.node_name when '开始' then t10.createdate else null end) as '开始',
 4 (case t10.node_name when '规划库' then t10.createdate else null end) as '规划库',
 5 (case t10.node_name when '投资' then t10.createdate else null end) as '投资',
 6 (case t10.node_name when '站点' then t10.createdate else null end) as '站点',
 7 (case t10.node_name when '方案' then t10.createdate else null end) as '方案',
 8 (case t10.node_name when '集成' then t10.createdate else null end) as '集成'
 9 into #temp10
10 from #temp as t10
11 
12 select * from #temp10
13 
14 select objectid,sitename,
15 max([开始]) as [开始],
16 max([规划库]) as [规划库],
17 max([投资]) as [投资],
18 max([站点]) as [站点],
19 max([方案]) as [方案],
20 max([集成]) as [集成]
21 into #temp12
22 from #temp10
23 group by objectid,sitename

对比之后两种方案结果一致。

执行效果:

2    1    2015-10-30 10:26:59.663    2015-10-30 10:26:59.683    2015-10-30 10:27:32.013    2015-10-30 16:37:14.153    2015-10-30 16:37:31.387    2015-10-30 16:38:51.797
4    1    2015-10-30 18:03:09.720    2015-10-30 18:03:09.737    2015-10-30 18:03:32.650    2015-10-30 21:31:11.683    2015-10-30 21:31:47.820    2015-10-30 21:32:28.450
6    1    2015-11-03 15:13:16.393    2015-11-03 15:13:16.410    2015-11-03 15:13:40.603    2015-11-03 15:14:30.073    2015-11-03 15:14:50.243    2015-11-03 15:18:15.117
8    1    2015-11-03 15:45:21.577    2015-11-03 15:45:21.593    2015-11-03 15:45:39.000    2015-11-03 15:46:08.953    2015-11-03 15:46:22.547    NULL
9    1    2015-11-03 16:26:01.500    2015-11-03 16:26:01.520    2015-11-03 16:26:22.587    NULL    NULL    NULL
10    1    2015-11-03 16:29:10.687    2015-11-03 16:29:10.697    2015-11-03 16:29:28.677    NULL    NULL    NULL
11    1    2015-11-03 16:33:02.083    2015-11-03 16:33:02.107    2015-11-03 16:33:23.270    2015-11-03 16:33:59.510    2015-11-03 16:41:48.663    2015-11-03 16:42:14.450
5    2    2015-11-03 10:31:13.560    2015-11-03 10:31:13.580    2015-11-03 10:34:41.880    NULL    NULL    NULL
7    2    2015-11-03 15:30:07.367    2015-11-03 15:30:07.377    2015-11-03 15:30:31.943    2015-11-03 15:31:29.560    2015-11-03 15:31:45.950    2015-11-03 15:32:08.257
3    7    2015-10-30 17:36:29.193    2015-10-30 17:36:29.237    2015-10-30 17:36:54.887    2015-10-30 17:37:24.507    2015-10-30 17:37:47.057    NULL

 

posted @ 2015-11-04 14:21  cctext  阅读(387)  评论(0编辑  收藏  举报