pivot 

可以把列值转换为输出中的多个列。

pivot 可以在其他剩余的列的值上执行聚合函数。

unpivot

将列转换为列值

语法

SELECT <non-pivoted column>,
[first pivoted column] AS <column name>,
[second pivoted column] AS <column name>,
...
[last pivoted column] AS <column name>
FROM
(<SELECT query that produces the data>)
AS <alias for the source query>
PIVOT
(
<aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
IN ( [first pivoted column], [second pivoted column],
... [last pivoted column])
) AS <alias for the pivot table>
<optional ORDER BY clause>;

示例1:pivot

1.数据准备

create table student_score
(
studentId varchar(50),
subjectName varchar(50),
score decimal(18)
) insert into student_score values
('','语文',80),('','数学',70),('','英语',90),
('','语文',80),('','数学',83),('','英语',60),
('','语文',50),('','数学',90),('','英语',60),
('','语文',90),('','数学',80)

按学生id分组查看平均成绩

select studentId,AVG(score) avgScore from student_score
group by studentId

初始效果

2.使用pivot

select 'averagescore' as avgScore_by_studentId,
[],[],[],[]
from
(
select studentId,score
from student_score
) as sourceTable
pivot
(
AVG(score) for studentId in ([],[],[],[])
) as pivotTable

3.效果

示例2:unpivot 

1.数据准备

-- Create the table and insert values as portrayed in the previous example.
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
Emp3 int, Emp4 int, Emp5 int);
GO
INSERT INTO pvt VALUES (1,4,3,5,4,4);
INSERT INTO pvt VALUES (2,4,1,5,5,5);
INSERT INTO pvt VALUES (3,4,3,5,4,4);
INSERT INTO pvt VALUES (4,4,2,5,5,4);
INSERT INTO pvt VALUES (5,5,1,5,5,5);
GO

初始效果

表示供应商(vendorID)在用户1(Emp1)中的订单数量,其他类比即可。

2.使用示例

-- Unpivot the table.
SELECT VendorID, Employee, Orders
FROM
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt;

3.效果

参考网址

sql学习~pivot和unpivot用法的更多相关文章

  1. 通过sql做数据透视表,数据库表行列转换(pivot和Unpivot用法)(一)

    在mssql中大家都知道可以使用pivot来统计数据,实现像excel的透视表功能 一.MSsqlserver中我们通常的用法 1.Sqlserver数据库测试 ---创建测试表 Create tab ...

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

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

  3. SQL学习:查询的用法(1)

    在SQL servre的使用中,查询的用法是最多的.最重要的,也是最难学习的,因此掌握查询的用法很重要. 先将表的示例上图 员工表: 部门表:                             ...

  4. SQL中PIVOT和UNPIVOT行列转换

    DECLARE @sql_col VARCHAR(8000); DECLARE @sql_str VARCHAR(8000); DECLARE @sql_ VARCHAR(MAX); SELECT @ ...

  5. sql pivot、unpivot和partition by用法

    原文:sql pivot.unpivot和partition by用法 演示脚本 from sys.sysobjects where name = 'Student' AND type = 'U') ...

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

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

  7. SQL Server 行列相互转换命令:PIVOT和UNPIVOT使用详解

    一.使用PIVOT和UNPIVOT命令的SQL Server版本要求 1.数据库的最低版本要求为SQL Server 2005 或更高. 2.必须将数据库的兼容级别设置为90 或更高. 3.查看我的数 ...

  8. PIVOT 和 UNPIVOT 命令的SQL Server版本

    I:使用 PIVOT 和 UNPIVOT 命令的SQL Server版本要求 1.数据库的最低版本要求为 SQL Server 2005 或 更高 2.必须将数据库的兼容级别设置为 90 或 更高 3 ...

  9. Oracle 行列转换函数pivot、unpivot的使用(二)

    一.行转列pivot 关键函数pivot,其用法如下 pivot(聚合函数 for 列名 in(类型)) select * from table_name pivot(max(column_name) ...

随机推荐

  1. LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解

    题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一 ...

  2. AF(操作者框架)系列(3)-创建第一个Actor的程序

    这节课的内容,语言描述基本是无趣的,就是一个纯程序编写,直接上图了. 如果想做其他练习,可参考前面的文章: https://zhuanlan.zhihu.com/p/105133597 1. 新建一个 ...

  3. SpringBoot2整合Redis缓存

    遵循SpringBoot三板斧 第一步加依赖 <!-- Redis --> <dependency> <groupId>org.springframework.bo ...

  4. unittest实战(三):用例编写

    # coding:utf-8import unittestfrom selenium import webdriverimport timefrom ddt import ddt, data, unp ...

  5. 关于css 的垂直居中

    对于元素的水平居中,我根据我自己之前的一些学习来进行一些总结,如果有不对的地方,欢迎指正~ 一.让大小不固定的元素垂直居中 因为:表格的单元格的特别属性:垂直居中等: `div.parent { di ...

  6. ionic监听android返回键(实现“再按一次退出”功能)

    在android平台上的app,在主页面时经常会遇到"再按一次退出app"的功能,避免只按一下返回键就退出app提升体验优化. 1.这个功能需要我们用到ionic提供的regist ...

  7. Markdown语法说明及常用软件推荐(附链接)

    Markdown语法同样支持HTML标签 以下所有字符均为英文字符 标题 标题级别由#决定,一个为一级 样例 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ...

  8. angular - collapse--TemplateRef

    用的ant  collapse组件 需要自定义Header 请忽略前面的dw... 主要是TemplateRef<void> 查了半天.可以实现效果,直接上代码,我也不懂为什么 反正实现了 ...

  9. js中 __proto__ 和 prototype

    js中的对象都有__proto__属性存在[隐式原型],注意是两个_, 1,javascript对象的__proto__指向的是该对象的构造函数的原型对象,即constructor.prototype ...

  10. openwrt sdk 添加软件包 Makefile 写法

    参考 https://openwrt.org/start?id=docs/guide-developer/packages ,英文稍好点的自己看吧,我写出来也就是方便,英文不好的人看. 软件包的来源, ...