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) 创建数据表的时候 ...
随机推荐
- 2016 - 1 - 24 CSS初步
1.The difference between CSS and HTML HTML document is that it specities the content of the page. An ...
- Jena TDB 101 Java API without Assembler
Update on 2015/05/12 ongoing tutorials site on https://github.com/zhoujiagen/semanticWebTutorialUsin ...
- JAVA修饰符类型(public,protected,private,friendly)
转自:http://www.cnblogs.com/webapplee/p/3771708.html JAVA修饰符类型(public,protected,private,friendly) publ ...
- CF 600B Queries about less or equal elements --- 二分查找
CF 600B 题目大意:给定n,m,数组a(n个数),数组b(m个数),对每一个数组b中的元素,求数组a中小于等于数组该元素的个数. 解题思路:对数组a进行排序,然后对每一个元素b[i],在数组a中 ...
- chrome 49 版本 跨越 --args --disable-web-security --user-data-dir
转载: 做前端的,用Ajax获取数据,是常有的事情,同域下自然没问题了,如果是不同域获取数据,浏览器就有个同源策略的限制. 如图: Origin * is not allowed by Access- ...
- QString转换为char* (转)
Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...
- Centreon 监控报警
1.系统更新:yum update 2.安装组件:yum install -y httpd php-pear php php-mysql php-gd php-ldap php-xml php-mbs ...
- Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
#!/usr/bin/python# -*- coding: utf-8 -*- 解决方法: 可以看到我的版本是2.6的,所以打开/usr/lib64/python2.6/site.py 红框里本来是 ...
- npm以及gulp相关操作
在工作流相关的第一篇博客中,我们安装了nodejs的环境,那么nodejs自带的npm是一个功能十分强大的管理器,它已经不仅仅是局限于nodejs的版本管理器了,那么当现在我们可以通过npm来下载我们 ...
- gauss消元
题意描述:有n个星球,m台望远镜.每台望远镜有一个开始时间和结束时间,但只给出了月.日的信息,没有给出年份,每台望远镜记录了它所观测的星球上发生的各类事件的次数.每类事件持续的时间是恒定的,且不会超过 ...