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=

随便看看

NodeJs使用jwt生成token以及使用express-jwt校验和解密token

=0){//当数据库有当前用户时,它返回tokenlettoken=jwt.sign;res.send}else{res.send}}catch{//p抛出异常并将其发送到错误中间件以处理console.log;next;}})//注册接口路由器。post('/register',异步(req,res,next)=˃{let{用户名,密码,昵称}=req-b...

【01】如何在XMind中排列自由主题

如何在XMind中安排免费主题。在XMind思维导图软件中,用户可以根据需要添加免费主题。然而,由于自由主题的灵活性,它并不整洁,与需要控制界面有序排列的用户相比,这会造成一定的麻烦。首先选择要组织的所有免费主题,单击,然后在下拉框中选择以安排免费主题。有六种排列方式:左对齐、垂直居中、右对齐、顶部对齐、水平居中和底部对齐。...

Vue浏览器调试工具VueTools安装以及使用

ue-devtools是一款基于chrome浏览器的插件,用于vue应用的调试,这款vue调试神器可以极大地提高我们的调试效率。vue-devtools使用起来还是比较简单的,上手非常的容易,这里就细讲其使用说明了。安装方法二:这里以chrome浏览器为例:1、打开chrome网上应用店,搜索vue.js注:如果打不开页面需要代理选择第一个,点击添加至chr...

oracle报ORA-08103: 对象不再存在错误

今天,在导入数据时,数据库多次抛出异常。最常见的原因是同时操作。有人截断了表,然后将其放入选择表中。在群里发布错误,有人立刻出来认出他,说他正在整理表格……如果不是因为这个原因,百度也给出了其他尝试的计划:再次执行;查询时添加架构。...

H3C交换机如何配置管理VLAN

1.输入“系统视图”(缩写为“sys”)进入系统配置模式[H3C]...

pageoffice

--PageOfficeEnd--˃4.这将更改为印章简单管理页面的管理员登录密码。为了安全起见,强烈建议更改它!!!如果您使用Oracle、MySQL和其他数据库,请删除以下节点上的注释,并将其修改为密封数据库连接参数。最好删除印章。WEB-INF库下的mdb。...