Ms sql pivot unpivot
--建表
create table dbo.orders
( orderid int not null primary key nonclustered,
orderdate datetime not null,
empid int not null,
custid varchar(5) not null,
qty int not null)
--插入数据
insert into orders values(30001,'',3,'a',10)
insert into orders values(10001,'',1,'a',12)
insert into orders values(10005,'',1,'b',20)
insert into orders values(40001,'',4,'a',40)
insert into orders values(10006,'',1,'c',14)
insert into orders values(20001,'',2,'b',12)
insert into orders values(40005,'',4,'a',10)
insert into orders values(20002,'',2,'c',20)
insert into orders values(30003,'',3,'b',15)
insert into orders values(30004,'',3,'c',22)
insert into orders values(30007,'',3,'d',30) --数据。
select custid,year(orderdate) as orderdate ,qty from orders order by custid custid orderdate qty
------ ----------- -----------
a 2002 10
a 2002 12
a 2003 40
a 2004 10
b 2004 15
b 2003 12
b 2002 20
c 2002 22
c 2003 14
c 2004 20
d 2002 30
(11 行受影响) --转阿转。
select custid,sum(case when orderdate=2002 then qty end) as [],
sum(case when orderdate=2003 then qty end) as [],
sum(case when orderdate=2004 then qty end) as []
from ( select custid,year(orderdate) as orderdate ,qty from orders ) as d group by custid; custid 2002 2003 2004
------ ----------- ----------- -----------
a 22 40 10
b 20 12 15
c 22 14 20
d 30 NULL NULL
警告: 聚合或其他 SET 操作消除了空值。
(4 行受影响) --行转列
select * from (select custid,year(orderdate) as orderyear,qty from dbo.orders) as d
pivot (sum(qty) for orderyear in ([],[],[])) as p; custid 2002 2003 2004
------ ----------- ----------- -----------
a 22 40 10
b 20 12 15
c 22 14 20
d 30 NULL NULL
(4 行受影响) --反转。
select custid,orderyear,qty from pvtt unpivot ( qty for orderyear in ([],[],[])) as u; custid orderyear qty
------ -------------------------------------------------------------------------------------------------------------------------------- -----------
a 2002 22
a 2003 40
a 2004 10
b 2002 20
b 2003 12
b 2004 15
c 2002 22
c 2003 14
c 2004 20
d 2002 30
(10 行受影响) --动态反转。注意@b变量要放在括号里,否则报不是有效的标识符
declare @a varchar(4000),@b varchar(4000)
set @a=''
select @a=@a+'],['+cast(a as varchar(10)) from (select distinct year(orderdate) as a from orders) as b
set @a=right(@a,len(@a)-2)+']'
set @b='select custid,orderyear,qty from pvtt unpivot (qty for orderyear in ('+ @a +')) as c'
exec (@b) custid orderyear qty
------ ----------------------------------------------------------------------------------------
a 2002 22
a 2003 40
a 2004 10
b 2002 20
b 2003 12
b 2004 15
c 2002 22
c 2003 14
c 2004 20
d 2002 30
(10 行受影响) --动态反转,注意DISTINCT引用否则则提示多次引用列。
declare @a varchar(4000),@b varchar(4000)
set @a=''
select @a=@a+aa from (select distinct (select '['+cast ( a as varchar(10))+'],' as [text()] from (select distinct year(orderdate) as a from orders
) as a3 for xml path('')) as aa from (select distinct year(orderdate) as a from orders
) as a2) as c
set @a=left(@a,len(@a)-1)
set @b='select custid,orderyear,qty from pvtt unpivot (qty for orderyear in ('+ @a +')) as c'
exec (@b) custid orderyear qty
------ --------------------------------------------------------------------------------------
a 2002 22
a 2003 40
a 2004 10
b 2002 20
b 2003 12
b 2004 15
c 2002 22
c 2003 14
c 2004 20
d 2002 30
(10 行受影响) --通过系统视图(INFORMAT_SCHEMA.COLUMNS)拉列表。
declare @a as table( y int not null primary key );
declare @cols as nvarchar(max)
declare @sql as nvarchar(max)
set @cols=stuff(
(select N','+quotename(y) as [text()] from
(select column_name as y from INFORMATION_SCHEMA.COLUMNS
where table_schema=N'dbo' and table_name=N'pvtt' and column_name not in (N'custid')) as y
order by y for xml path('')),1 ,1 ,N'');
set @sql=N'select custid,orderyear,qty from dbo.pvtt unpivot(qty for orderyear in ('+@cols+N')) as u;';
exec sp_executesql @sql custid orderyear qty
------ -------------------------------------------------------------------------------------
a 2002 22
a 2003 40
a 2004 10
b 2002 20
b 2003 12
b 2004 15
c 2002 22
c 2003 14
c 2004 20
d 2002 30
(10 行受影响)
Ms sql pivot unpivot的更多相关文章
- MS SQL PIVOT数据透视表
以前曾经做过练习<T-SQL PIVOT 行列转换>https://www.cnblogs.com/insus/archive/2011/03/05/1971446.html 今天把拿出来 ...
- SQL Server中行列转换 Pivot UnPivot
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...
- sql pivot、unpivot和partition by用法
原文:sql pivot.unpivot和partition by用法 演示脚本 from sys.sysobjects where name = 'Student' AND type = 'U') ...
- sql server 行转列 Pivot UnPivot
SQL Server中行列转换 Pivot UnPivot 本文转自:张志涛 原文地址: http://www.cnblogs.com/zhangzt/archive/2010/07/29/17878 ...
- MS SQL巡检系列——检查外键字段是否缺少索引
前言感想:一时兴起,突然想写一个关于MS SQL的巡检系列方面的文章,因为我觉得这方面的知识分享是有价值,也是非常有意义的.一方面,很多经验不足的人,对于巡检有点茫然,不知道要从哪些方面巡检,另外一方 ...
- MS SQL巡检系列——检查重复索引
前言感想:一时兴起,突然想写一个关于MS SQL的巡检系列方面的文章,因为我觉得这方面的知识分享是有价值,也是非常有意义的.一方面,很多经验不足的人,对于巡检有点茫然,不知道要从哪些方面巡检,另外一方 ...
- [MS SQL Server]SQL Server如何开启远程访问
在日常工作中,经常需要连接到远程的MS SQL Server数据库中.当然也经常会出现下面的连接错误. 解决方法: 1. 设置数据库允许远程连接,数据库实例名-->右键--->属性---C ...
- Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)
Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码) http://blog.csdn.net/helonsy/article/details/7207497 ...
- MS SQL Server中数据表、视图、函数/方法、存储过程是否存在判断及创建
前言 在操作数据库的时候经常会用到判断数据表.视图.函数/方法.存储过程是否存在,若存在,则需要删除后再重新创建.以下是MS SQL Server中的示例代码. 数据表(Table) 创建数据表的时候 ...
随机推荐
- xml+js+html的二级联动
首先需要准备的文档是: cities.xml //主要是标注中国各省及其各省下的各个城市 内容如下: <?xml version="1.0" encoding="U ...
- 寻找研究基于NS2研究覆盖网络的小伙伴:)
如题,本人菜鸟刚刚入门,想找些基于NS2研究覆盖网络方面的小伙伴,具体点是关于覆盖网络中QoS服务调度方法方面的,有的小伙伴可以留下联系方式,或者加我QQ:245939069 :P:P:P
- PCL中point cloud的数据类型
出处: http://wiki.ros.org/pcl/Overview 1.数据类型 1.1 ROS中point cloud数据类型 sensor mesgs::PointCloud sensor ...
- C# 多线程同步和线程通信
多线程通信 1. 当线程之间有先后的依赖关系时,属于线程之间的通信问题.也就是后一个线程要等待别的一个或多个线程全部完成,才能开始下一步的工作.可以使用: WaitHandle Class WaitH ...
- MySQL数据单个数据太大,导入不进去
mysql导入数据,navicat报错: MySQL server has gone away Table Restored: act_ge_bytearray Rolling back... Fin ...
- jQuery UI Autocomplete是jQuery UI的自动完成组件(share)
官网:http://jqueryui.com/autocomplete/ 以下分享自:http://www.cnblogs.com/yuzhongwusan/archive/2012/06/04/25 ...
- [debian]SublimeText>PrettyCode無效
怣 apt-get install node http://nodejs.org/#download.
- 2015GitWebRTC编译实录12
2015.07.20 libjingle_peerconnection 编译通过[1382/1600 ] CXX obj/talk/app /webrtc/libjingle_peerconnecti ...
- dashboard
http://www.htmleaf.com/pins/chart-doc/index.html http://www.flotcharts.org/flot/examples/ https://gi ...
- 《C与指针》第四章练习
本章问题 1.Is the following statement legal?If so,what does it do? (下面的语句是否合法,如果合法,它做了什么) 3 * x * x - 4 ...