SQL2008 R2 SP3中使用pivot实现行转列
同事遇到一个问题,怎么实现行转列,参考了别人的示例,我是这样解决的。
创建数据源:
create table #temp (
objectid int,
sitename nvarchar(32),
cities nvarchar(32),
projectdurationno nvarchar(32),
fullname nvarchar(32),
createdate datetime,
nodename nvarchar(32)
) insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'','海口市','','','10 30 2015 10:26AM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'','海口市','','','10 30 2015 10:26AM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'','海口市','','','10 30 2015 10:27AM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'','海口市','','','10 30 2015 4:37PM','站点')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'','海口市','','','10 30 2015 4:37PM','方案')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(2,'','海口市','','','10 30 2015 4:38PM','集成')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'','海口市','','','10 30 2015 6:03PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'','海口市','','','10 30 2015 6:03PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'','海口市','','','10 30 2015 6:03PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'','海口市','','','10 30 2015 9:31PM','站点')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'','海口市','','','10 30 2015 9:31PM','方案')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'','海口市','','','10 30 2015 9:32PM','集成')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(4,'','海口市','','','10 30 2015 9:32PM','光缆厂家')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:13PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:13PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:13PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:14PM','站点')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:14PM','方案')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:18PM','集成')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:18PM','光缆厂家')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:19PM','分布系统、设备安装')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:19PM','光缆到位')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:24PM','PTN上线')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:25PM','开通')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(6,'','海口市','','','11 3 2015 3:26PM','结束')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'','海口市','','','11 3 2015 3:45PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'','海口市','','','11 3 2015 3:45PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'','海口市','','','11 3 2015 3:45PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'','海口市','','','11 3 2015 3:46PM','站点')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(8,'','海口市','','','11 3 2015 3:46PM','方案')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(9,'','海口市','','','11 3 2015 4:26PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(9,'','海口市','','','11 3 2015 4:26PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(9,'','海口市','','','11 3 2015 4:26PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(10,'','海口市','','','11 3 2015 4:29PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(10,'','海口市','','','11 3 2015 4:29PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(10,'','海口市','','','11 3 2015 4:29PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:33PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:33PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:33PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:33PM','站点')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:41PM','方案')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:42PM','集成')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:42PM','光缆厂家')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:42PM','分布系统、设备安装')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:43PM','光缆到位')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:44PM','PTN上线')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:44PM','开通')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(11,'','海口市','','','11 3 2015 4:46PM','结束')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'','海口市','','','11 3 2015 3:30PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'','海口市','','','11 3 2015 3:30PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'','海口市','','','11 3 2015 3:30PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'','海口市','','','11 3 2015 3:31PM','站点')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'','海口市','','','11 3 2015 3:31PM','方案')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'','海口市','','','11 3 2015 3:32PM','集成')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(7,'','海口市','','','11 3 2015 3:32PM','光缆厂家')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(5,'','海口市','','','11 3 2015 10:31AM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(5,'','海口市','','','11 3 2015 10:31AM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(5,'','海口市','','','11 3 2015 10:34AM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'','海口市','','','10 30 2015 5:36PM','开始')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'','海口市','','','10 30 2015 5:36PM','规划库')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'','海口市','','','10 30 2015 5:36PM','投资')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'','海口市','','','10 30 2015 5:37PM','站点')
insert into #temp(objectid,sitename,cities,projectdurationno,fullname,createdate,node_name)values(3,'','海口市','','','10 30 2015 5:37PM','方案')
方案一、使用pivot进行行转列:
select objectid,SiteName,[开始],[规划库],[投资],[站点],[方案],[集成]
from(
select objectid,sitename,NODE_NAME,createdate from #temp
) as t10
pivot(
max(createdate)
for node_name in
([开始],[规划库],[投资],[站点],[方案],[集成])
) as t11
order by sitename,objectid
方案二、group by 方案
select t10.sitename,t10.objectid,
(case t10.node_name when '开始' then t10.createdate else null end) as '开始',
(case t10.node_name when '规划库' then t10.createdate else null end) as '规划库',
(case t10.node_name when '投资' then t10.createdate else null end) as '投资',
(case t10.node_name when '站点' then t10.createdate else null end) as '站点',
(case t10.node_name when '方案' then t10.createdate else null end) as '方案',
(case t10.node_name when '集成' then t10.createdate else null end) as '集成'
into #temp10
from #temp as t10 select * from #temp10 select objectid,sitename,
max([开始]) as [开始],
max([规划库]) as [规划库],
max([投资]) as [投资],
max([站点]) as [站点],
max([方案]) as [方案],
max([集成]) as [集成]
into #temp12
from #temp10
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
SQL2008 R2 SP3中使用pivot实现行转列的更多相关文章
- PIVOT就是行转列,UNPIVOT就是列传行
PIVOT通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合.UNPIVOT与PIVOT执行相反的操作,将表值表达式的列转换为列值. 通 ...
- SQL SERVER pivot(行转列),unpivot(列转行)
[pivot]行转列:多行变一列 假设学生成绩表Score1 Name Subject Score 小张 语文 88 小花 数学 89 小张 数学 90 Name 语文 数学 小花 null 89 小 ...
- sql pivot(行转列) 和unpivot(列转行)的用法
1.PIVOT用法(行转列) select * from Table_Score as a pivot (sum(score) for a.name in ([语文],[数学],[外语],[文综],[ ...
- [JS&Jquery]实现页面表格中相同内容的行或列合并
详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeak<script type=&qu ...
- C#/VB.NET 获取Excel中图片所在的行、列坐标位置
本文以C#和vb.net代码示例展示如何来获取Excel工作表中图片的坐标位置.这里的坐标位置是指图片左上角顶点所在的单元格行和列位置,横坐标即顶点所在的第几列.纵坐标即顶点所在的第几行.下面是获取图 ...
- C#读txt文件并写入二维数组中(txt数据行,列未知)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Datagridview 在基于文本的单元格中启用换行,自动调整行高列宽
将 DataGridViewCellStyle的 WrapMode 属性设置为 DataGridViewTriState 枚举值之一.下面的代码示例使用 System.Windows.Forms.Da ...
- Oracle用函数或PIVOT实现行转列
原数据: 目标数据: 实现代码: SELECT YEAR AS "年", SUM (DECODE (QUATER, 1, RATE, 0)) AS "季度一", ...
- 在EXCEL中如何同时冻结行与列?
鼠标所在的单元格的位置 ,决定了你冻结的行和列.如: 冻结第一行与第一列, 只需要将鼠标置于单元格在第二列,第二行. 点击冻结
随机推荐
- PHP不仅仅是PHP
PHP不仅仅是PHP PHP不仅仅是PHP. PHP的面试不仅仅会问到PHP语言本身(基本都是基础和细节),下面列举的主要是都是高级工程师的要求 比如:PHP中include和require的区别 ...
- [源码]随机获取虾米音乐song_id API文件
[源码]随机获取虾米音乐song_id API文件 January 11, 2015 注意:此API请放置于国内主机使用,如香港.北京等等,否则会提示:虾米音乐在您所处的国家或地区暂时无法使用 < ...
- vi 整行 多行 复制与粘贴
vi编辑器中的整行(多行)复制与粘贴就非常必要了. 1.复制 1)单行复制 在命令模式下,将光标移动到将要复制的行处,按“yy”进行复制: 2)多行复制 在命令模式下,将光标移动到将要复制的首行处,按 ...
- 自定义UserProvider,更改验证方法
新建UserProvider,如果继承EloquentUserProvider,注入model是必须的,或者去继承interface,自己实现一些方法 use Illuminate\Auth\Eloq ...
- BAE3.0上的java+tomcat+hibernate代码发布
在BAE上使用hibernate说起来也简单,但因为一个不小心,耽误了好几个小时. 百度文档中有说: http://developer.baidu.com/wiki/index.php?title=d ...
- 一个比较轻巧好用的js分页插件,可ajax可url
var pageNav = pageNav || {}; pageNav.fn = null; pageNav.pre = "pre"; pageNav.next = " ...
- 【转】下载量最高的 100 个 Laravel 扩展包推荐
说明 Laravel 另一个令人喜欢的地方,是拥有活跃的开发者社区,而活跃的开发者社区带来的,是繁华的扩展包生态. 本文对 Packagist 上打了 Laravel 标签 的扩展包进行整理,截止到现 ...
- 【转】CodeIgniter定义自己的Helper和Helper的方法
最近做程序,采用了PHP+CodeIgniter框架,总体来说这框架很轻而且上手也很快的. 首先,说下如何定义自己的Helper,个人理解helper其实就是定义函数方法. 如果要建个全新的自己的He ...
- 自动adsl拨号上网
@echo offmode con cols=35 lines=6 & color 5Btitle 开机连接宽带--设置工具 QQ1009693258echo 请稍候...VER|FIND & ...
- protobuf序列化、反序列化
引用dllprotobuf-net.rar /// <summary> /// buf序列化 /// </summary> public static String Seria ...