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) 创建数据表的时候 ...
随机推荐
- ViewHolder简洁写法
ViewHolder holder = null; if(convertView == null){ convertView = mInflater.i ...
- webshell
webshell就是以asp.php.jsp或者cgi等网页文件形式存在的一种命令执行环境,也可以将其称做为一种网页后门.黑客在入侵了一个网站后,通常会将asp或php后门文件与网站服务器WEB目录下 ...
- easyui datagrid
function initData(){ //数据列表 yftjsy=$("#yftjsy").datagrid({ url: '', fit:true, showFooter:f ...
- 团队开发——冲刺2.d
冲刺阶段二(第四天) 1.昨天做了什么? 把收集的图标进行统一整理,使用相同风格.类型,使界面更加美观. 2.今天准备做什么? 开始写测试计划书. 3.遇到什么困难? 关于昨天遇到的问题:在游戏界面加 ...
- C#数据结构
数据结构是相互之间存在一种或多种特定关系的数据元素的集合.在任何问题中,数据元素之间都不是孤立的,而是存在着一定的关系,这种关系称为结构(Structure).根据数据元素之间关系的不同特性,通常有 ...
- Caffe proto閱讀
--------------------------------------------------------------------------------- 1.Data Layer sourc ...
- ps esc 问题
最近经常发现esc键突然变得不能用了.主要是使用搜狗输入法时使用esc键取输错的字,因此还以为是搜狗的问题,后来突然想到可能是因为打开某个软件导致esc不能用,最后发现居然是ps,在网上查了后发现很多 ...
- ios项目接入sdk事项
使用cocos2d-x引擎创建的项目在xcode里可以看到都带有一个ios目录,把要接入的sdk的包含.framework库文件和.bundle的资源文件的父目录拖入到xcode项目里的这个ios目录 ...
- frame和bounds区别
学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bound的 ...
- odoo.cli.main()做了什么?
先把代码贴过来 def main(): args = sys.argv[1:] # The only shared option is '--addons-path=' needed to disco ...