pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用

摘要:
转移自:http://blog.sina.com.cn/s/blog_5ef755720100cyo3.htmlpivot函数:createtabletest(idint,namevarchar(20),quarint,profileint)插入测试值(1,'a',11000)

转自:http://blog.sina.com.cn/s/blog_5ef755720100cyo3.html

pivot函数:

create table test(id int,name varchar(20),quarter int,profile int)
insert into test values(1,'a',1,1000)
insert into test values(1,'a',2,2000)
insert into test values(1,'a',3,4000)
insert into test values(1,'a',4,5000)
insert into test values(2,'b',1,3000)
insert into test values(2,'b',2,3500)
insert into test values(2,'b',3,4200)
insert into test values(2,'b',4,5500)

select * from test    --创建表test

pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用第1张

现在需要把quarter 从1列数据变成4列数据  效果如:

pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用第2张

把一列拆成几列这时候就能使用pivot函数很简单的实现

select * from test
pivot
(
 sum([profile]) for [quarter]
 in
 ([1],[2],[3],[4])
)
as
s

注:使用pivot把一列拆成几列时 需要后面as取个别名 这是固定的格式 同时如 for前是必须使用聚合函数的

当然不使用pivot函数也可以得到相同效果 只是代码长切效率低 但容易理解

select id,[name],
'1'=(select sum([profile]) from test where id=a.id and quarter=1),
'2'=(select sum([profile]) from test where id=a.id and quarter=2),
'3'=(select sum([profile]) from test where id=a.id and quarter=3),
'4'=(select sum([profile]) from test where id=a.id and quarter=4)
from test as a
group by id,name

-----------------------------------------------------------------------------------------

unpivot函数 顾名思义 他就是把几列合并到1列中去

create table test1(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int)

insert into test1 values(1,'a',1000,2000,4000,5000)
insert into test1 values(2,'b',3000,3500,4200,5500)

select * from test1 --创建test1表

pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用第3张

我们要把Q1 Q2 Q3 Q4合到1列 季度列中去 如效果:

pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用第4张

使用unpivot可以很简单的实现

select id ,[name],[jidu],[xiaoshou] from test1
unpivot
(
 xiaoshou for jidu in
 ([q1],[q2],[q3],[q4])
)
as f

注:同样需要使用as取别名同样是固定的格式 unpivot函数中没有聚合函数 xiaoshou和jidu列都是原来没有的 jidu表由原来的Q1 Q2 Q3 Q4组成 

同样的不使用unpivot也可以实现以上的功能

select id,[name],
jidu='Q1',
xiaoshou=(select Q1 from test1 where id=a.id)
from test1 as a
union
select id,[name],
jidu='Q2',
xiaoshou=(select Q2 from test1 where id=a.id)
from test1 as a
union
select id,[name],
jidu='Q3',
xiaoshou=(select Q3 from test1 where id=a.id)
from test1 as a
union
select id,[name],
jidu='Q4',
xiaoshou=(select Q4 from test1 where id=a.id)
from test1 as a

免责声明:文章转载自《pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇继承QAbstractTableModel QStyledItemDelegate实现自定义表格,添加进度条和选中框。【插件笔记】两款短小精悍的滚动插件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

C#控件重绘学习(一)

由于需要制作工业控制软件,传统的控制已不能满足实际要求,因此控制的重新绘制迫在眉睫。因为考研花费了很多时间,而C#的学习已经搁浅了很长时间,所以我打算借此机会仔细彻底地研究控件的重新绘制。首先,查看Button的旧背景:namespace:SystemWindows.Forms程序集:System.Windows.FormsButton具有太多属性:例如Ba...

zlog 使用手册

Zlog是一个纯C日志函数库,具有高可靠性、高性能、线程安全性、灵活性和清晰的概念。Syslog是一个系统级的轮子,但它的速度慢,功能单调。Zlog比log4c更高效、更实用、更安全,它是用c编写的。Zlog使用了C99兼容的vsnprintf。...

iview表格动态数据实现合并功能

需求原型:代码实现:html part:从'../../libs/c导入{MsgType,PublicType}...

JavaMail给QQ邮箱发邮件报错

org.springframework.mail.MailAuthenticationException:身份验证失败;nestedexceptionisjavax.mail.AuthenticationFailedException:535错误:http://service.mail.qq.com/cgi-bin/help?subtype=1&&a...

微信小程序的模板消息与小程序订阅消息

有关获取分发权限的更多信息,请参阅applet侧消息订阅接口wx的步骤3。requestSubscribeMessage。有关发出订阅消息的调用接口的更多信息,请参阅服务器端消息发送接口subscribeMessage。sendwx。requestSubscribeMessage(Objectobject)基本库2.8.2。必须填写参数Objectobjec...

vue 获取元素高度

1、html2、JavaScript//获取高度值(内容高+padding+边框)letheight=this.$refs.getheight.offsetHeight;//获取元素样式值(存在单位)letheight=window.getComputedStyle(this.$refs.getheight).height;//获...