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中如何同时冻结行与列?
鼠标所在的单元格的位置 ,决定了你冻结的行和列.如: 冻结第一行与第一列, 只需要将鼠标置于单元格在第二列,第二行. 点击冻结
随机推荐
- Solr 连接数据库
实际工程应用中,从数据库导出数据创建索引再常见不过了,现在实验一下从数据库导入数据创建索引. 一.版本说明 Solr版本:4.7.0 数据库:sqlserver2005 二.配置步骤 1. 准备的j ...
- Types of compression algorithms
http://www.html5rocks.com/en/tutorials/speed/img-compression/ Types of compression algorithms There ...
- Delphi深度探索-CodeSite应用指南
Delphi深度探索-CodeSite应用指南 Delphi虽然为我们提供极其强大的调试功能,查找Bug仍然是一项艰巨的工作,通常我们写代码和调试代码的所消耗的时间是大致相同的,甚至有可能更多.为了减 ...
- The Producer-Consumer Relationship
//Listing 3-1. The Producer-Consumer Relationship Version 1 public class PC { public static void mai ...
- Qt中单例模式的实现(4种方法)
最简单的写法: 12345 static MyClass* MyClass::Instance(){ static MyClass inst; return &inst;} 过去很长一段时间一 ...
- .NET对象与Windows句柄(二):句柄分类和.NET句柄泄露的例子
上一篇文章介绍了句柄的基本概念,也描述了C#中创建文件句柄的过程.我们已经知道句柄代表Windows内部对象,文件对象就是其中一种,但显然系统中还有更多其它类型的对象.本文将简单介绍Windows对象 ...
- UML时序图总结
前言 在我的工作中,用的最多的就是时序图了.可能由于工作的原因,我也是最喜欢画时序图了,很清楚,很明了,什么时候发送什么消息,到达什么状态,一下子就展示在你的脑海里,对于消息驱动的程序来说,是再好不过 ...
- 50道JavaScript经典题和解法(JS新手进...持续更新...)
最近在学习<数据结构与算法JavaScript描述>这本书,对JavaScript的特性和数据结构都有了进一步的了解和体会. 学习之余,也进行了相应的练习,题目难度不大,但是对所学知识的巩 ...
- How to disable and clear query ranges in sysquery form
query = new query('Query name'); queryBuildDataSource = query.dataSourceTable(tableNum('table name') ...
- sqlserver 中含有某字符串
查找 sqlserver 中字符串的ascii码SET TEXTSIZE 0-- Create variables for the character string and for the curre ...