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. idea 的一些基本的配置

    1. 2. 3. http://139.199.89.239:1008/88414687-3b91-4286-89ba-2dc813b107ce 531403098@qq.com R+0oOAb3rx ...

  2. 从零开始学Python 二

    上一章我们已经安装好了Python环境,并且写出了第一个Python程序.下面我们接着继续学习.首先,来分析下上一章我们写的代码. 我们最初的目的是为了可以输出一串有意义的文字,最终选择了英文语句“h ...

  3. java数组冒泡排序

    public class PaiXu1 { public static void main(String[] args) { int[] s = {345,879,456,870,221,902,14 ...

  4. git提示错误关于错误:ssh: Could not resolve hostname github.com: Name or service not known.fatal: Could not read from remote repository.

    关于 Git 使用中出现的错误 饥人谷_楠柒 关注 2016.11.02 15:33* 字数 746 阅读 3607评论 5喜欢 10赞赏 1 关于错误:ssh: Could not resolve ...

  5. JavaWeb+SVN+Maven+Tomcat +jenkins实现自动化部署

    网址:https://blog.csdn.net/liyong1028826685/article/details/88289218 在日常开发项目中常见的开发模式是使用代码库来存放我们的项目例如:S ...

  6. 关于Phabricator Arcanist以及提交项目代码

    git配置 github的使用:https://github.com/runchen0518/OnlineJudge/blob/master/README.md $ git config --glob ...

  7. Altium designer 新建快捷键

    示例: 1.按下Ctrl: 2.点击需要建立快捷键的图标:点击交互式布线图标,然后在选择性输入要用到的快捷键:

  8. 使用lamdba函数对list排序

    lamdba好处:精简代码,省去了定义函数.

  9. [转]sqlldr 导入乱码,Oracle客户端字符集问题

    1,查Oracle数据库创建时候的字符集:oracle服务器端执行 SQL> select name, value$ from sys.props$ where name like 'NLS%' ...

  10. Tkinter模块:Grid几何管理器

    Tkinter模块是Python的标准库模块之一,也是使用Python语言进行图形化用户界面(GUI)开发的基础. 本文介绍一下Tkinter模块的Grid几何管理器. 使用VB.MFC进行GUI开发 ...