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) 创建数据表的时候 ...
随机推荐
- 三部曲一(数据结构)-1024-Eqs
解方程整数解的题,通过这道题我学会了这种题的一般做法,对于未知数较少.取值范围较小且解为整数的方程,把一半的未知数移到等式的另一边,然后对两边分别枚举,用哈希配对,如果有相同的结果就找到一组解.具体做 ...
- 在Hibernate中配置Hilo进行数据绑定测试时出错:org.hibernate.MappingException: Could not instantiate id generator
在进行学习具体类单表继承时使用hilo类型时总是在调度过程中提示如下信息,无法通过.留下记录备查. 在网上找相关信息, 未解决,详细如下: org.hibernate.MappingException ...
- anjularjs 路由
在多视图单页面web应用中,angularjs使用路由‘#+标记’来区别不同的逻辑页面并将不同的页面绑定到对应的控制器上.通过一个简单的实例来深入理解: 1.index.html 主页面中插入代码: ...
- Web加载资源问题
Web加载静态资源的时候是同步加载的,每次加载必须前一个加载完成后进行后一个加载,这个是由于javascript 去阻塞浏览器其它操作导致的 推荐文章:http://www.cnblogs.com/l ...
- An internal error occured during :"C/C++" . java.lang.NullPointerException
用eclipse 导入cocos2d项目的时候报了这个错,导致项目在eclipse 里面是空的,反复导入也不行. 解决办法,把其他正常项目里面的proj.android目录下面的.cproject文件 ...
- viewpager接受值图片轮播
package com.baway.test; import java.util.ArrayList;import java.util.List;import java.util.Timer;impo ...
- Windows下启动,关闭Nginx命令
启动 直接点击Nginx目录下的nginx.exe 或者 cmd运行start nginx 关闭 nginx -s stop 或者 nginx -s quit stop表示立即 ...
- Android-Cdma手机定位
对于Android CDMA手机获取当前位置,其实有更便利的办法,就是哄骗CdmaCellLocation.getBaseStationLatitude(),然则getBaseStationLatit ...
- 原创:cellmap 基站查询 for android
cellmap for android 3.6.8.7.9.8 更新日期:2016年12月30日 特别声明:本软件不能进行手机定位,不能对手机号码定位,谨防被骗. 下载地址: cellmap3.6.8 ...
- [Linux] VirtualBox - 主机与虚拟机互通 - CentOS
使用VirtualBox的主机与虚拟机相互通信方法: (使用VirtualBox不要使用绿色版的,因为绿色版的没有安装虚拟网卡驱动,所以主机与虚拟机是不能相互通信的,切记) 1.设置已经安装好的操作系 ...