pivot 与 unpivot 函数是SQL05新提供的2个函数  

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

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

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

把一列拆成几列这时候就能使用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表

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

使用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个函数的更多相关文章

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

    转自:http://blog.sina.com.cn/s/blog_5ef755720100cyo3.html pivot函数: create table test(id int,name varch ...

  2. pivot 与 unpivot函数

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

  3. ES6入门五:箭头函数、函数与ES6新语法

    箭头函数的基本用法与特点 函数与ES6新语法 一.箭头函数的基本用法与特点 声明箭头函数采用声明变量和常量的关键字:var.let.const 箭头函数的参数:没有参数用"()"空 ...

  4. 使用 PIVOT 和 UNPIVOT 行转列 列转行 报表统计 函数

    官方文档:http://technet.microsoft.com/zh-cn/library/ms177410(v=SQL.105).aspx 可以使用 PIVOT 和 UNPIVOT 关系运算符将 ...

  5. [转]Oracle SQL函数pivot、unpivot转置函数实现行转列、列转行

    原文地址:http://blog.csdn.net/seandba/article/details/72730657 函数PIVOT.UNPIVOT转置函数实现行转列.列转行,效果如下图所示: 1.P ...

  6. Oracle11g 行列转换函数PIVOT and UNPIVOT

    作为Oracle开发工程师,推荐大伙看看 PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1 This article shows ...

  7. SQL(横表和纵表)行列转换,PIVOT与UNPIVOT的区别和使用方法举例,合并列的例子

    使用过SQL Server 2000的人都知道,要想实现行列转换,必须综合利用聚合函数和动态SQL,具体实现起来需要一定的技巧,而在SQL Server 2005中,使用新引进的关键字PIVOT/UN ...

  8. [转]使用 PIVOT 和 UNPIVOT

    可以使用 PIVOT 和 UNPIVOT 关系运算符将表值表达式更改为另一个表.PIVOT 通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执 ...

  9. 使用 PIVOT 和 UNPIVOT

    http://msdn.microsoft.com/zh-cn/library/ms177410%28v=SQL.90%29.aspx 可以使用 PIVOT 和 UNPIVOT 关系运算符将表值表达式 ...

随机推荐

  1. 《网络是怎样连接的》PDF电子版书籍分享

    资料下载地址: 链接:https://pan.baidu.com/s/15tN9klTEsu-mQLayxI979g 提取码:ptu1 封面如下所示:

  2. Shell 脚本练习

    [第一个] #!/bin/bash#每个用户的总充值和消费以及剩余. cat yuanbao.txt |grep -v 2016 |awk '{print $3}' |awk '!a[$0]++' & ...

  3. mysql中用limit 进行分页有两种方式

    代码示例:语句1: select * from student limit 9,4 语句2: slect * from student limit 4 offset 9 // 语句1和2均返回表stu ...

  4. Win10系列:C#应用控件进阶6

    路径 路径(Path)可以用来定义任意形状的曲线和几何图形,当然这种任意性也带来了复杂性.为了方便的绘制几何图形,微软在Visual Studio 2012安装包中为程序开发者提供了免费的Blend ...

  5. Angular2+ 编译后部署到服务器上页面刷新404问题

    原因:NG2+ 会默认不显示URL后面的文件名 解决方案:使用LocationStrategy方式,然后把URL后的# 替换成index.html# app.module.ts import {Has ...

  6. Saku实力挖坑记!!(十八)

    Saiku实力挖坑记!!!!!!! 我可真真真的是个挖坑小能手呀!不知道你们有没有遇到过这个异常: Enclosure class mondrian.olap.MondrianDef not foun ...

  7. 前端车牌识别SDK算法提取

    同行业中,别人标配有的产品我有,别人没有的产品我们也有,如此才能增强竞争力,通过优化创新,前端车牌识别SDK功能,性能上,都是行业NO.1的水平.车牌识别sdk这个用于越来越多人集成了,汽车保有量日益 ...

  8. ng-repeat 中的track by

    <div style="font-size:15px"ng-repeat="item in groups.items track by $index"&g ...

  9. 基于react的MQ管理系统遇到的问题以及解决办法

    跟一般的管理系统差不多 一.需求 1.新增申请和修改的弹窗里面的部门跟应用组改成下拉框,并调取后台接口获取到相应的值.1.1相应的代码 // 获取部门 getDepartment = () => ...

  10. shell练习题4

    需求如下: 系统logrotate工具,可以完成日志切割.归档.写一个shell脚本实现类似功能. 举例:假如服务的输出日志是1.log,要求每天归档一个,1.log第二天就变成1.log.1, 第三 ...