--语法形式:   
--SELECT
--<非透视的列>, [第一个透视的列] AS <列名称>,[第二个透视的列] AS <列名称>,...[最后一个透视的列] AS <列名称>,
--FROM
--(<生成数据的 SELECT 查询>) AS <源查询的别名>
--PIVOT
--(
--<聚合函数>(<要聚合的列>)
--FOR
--[<包含要成为列标题的值的列>]
--IN ( [第一个透视的列], [第二个透视的列],... [最后一个透视的列])
--) AS <透视表的别名>
--<可选的 ORDER BY 子句>--解释:       下面的例子能很好的说明
--常用的使用场景: 客户的订单量按照月份显示
--1.创建测试表
create table TradeForTest
(
customer varchar(20),
date datetime,
quantity int
)--2.插入测试数据
insert into TradeForTest(customer,date,quantity) values(1,'2018-01-01',98)
insert into TradeForTest(customer,date,quantity) values(1,'2018-03-01',80)
insert into TradeForTest(customer,date,quantity) values(1,'2018-05-01',90)
insert into TradeForTest(customer,date,quantity) values(2,'2018-02-01',88)
insert into TradeForTest(customer,date,quantity) values(2,'2018-04-01',86)
insert into TradeForTest(customer,date,quantity) values(2,'2018-06-01',88)
insert into TradeForTest(customer,date,quantity) values(3,'2018-05-01',60)
insert into TradeForTest(customer,date,quantity) values(3,'2018-05-01',86)
insert into TradeForTest(customer,date,quantity) values(3,'2018-05-01',88)
insert into TradeForTest(customer,date,quantity) values(4,'2018-04-01',74)
insert into TradeForTest(customer,date,quantity) values(4,'2018-06-01',99)
insert into TradeForTest(customer,date,quantity) values(4,'2018-08-01',59)
insert into TradeForTest(customer,date,quantity) values(5,'2018-10-01',96)-- case when 实现
select customer,
sum(case when MONTH(date)=1 then quantity else 0 end) as '一月份',
sum(case when MONTH(date)=2 then quantity else 0 end) as '二月份',
sum(case when MONTH(date)=3 then quantity else 0 end) as '三月份',
sum(case when MONTH(date)=4 then quantity else 0 end) as '四月份',
sum(case when MONTH(date)=5 then quantity else 0 end) as '五月份',
sum(case when MONTH(date)=6 then quantity else 0 end) as '六月份',
sum(case when MONTH(date)=7 then quantity else 0 end) as '七月份',
sum(case when MONTH(date)=8 then quantity else 0 end) as '八月份',
sum(case when MONTH(date)=9 then quantity else 0 end) as '九月份',
sum(case when MONTH(date)=10 then quantity else 0 end) as '十月份',
sum(case when MONTH(date)=11 then quantity else 0 end) as '十一月份',
sum(case when MONTH(date)=12 then quantity else 0 end) as '十二月份'
from TradeForTest group by customer--pivot 实现
select customer,
ISNULL([1],0) as '一月份',
ISNULL([2],0) as '二月份',
ISNULL([3],0) as '三月份',
ISNULL([4],0) as '四月份',
ISNULL([5],0) as '五月份',
ISNULL([6],0) as '六月份',
ISNULL([7],0) as '七月份',
ISNULL([8],0) as '八月份',
ISNULL([9],0) as '九月份',
ISNULL([10],0) as '十月份',
ISNULL([11],0) as '十一月份',
ISNULL([12],0) as '十二月份'
from (select customer,Month(date) date,quantity from TradeForTest )
as p
pivot
(
sum(quantity) for
p.date in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) a--5.删除表
truncate table TradeForTest
drop table TradeForTest

结果:

注意:

对升级到 SQL Server 2005 或更高版本的数据库使用 PIVOT 和 UNPIVOT 时,必须将数据库的兼容级别设置为 90 或更高
--法一:
ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = 90
GO
--法二:
EXEC sp_dbcmptlevel database_name,90
GO

转载于:https://www.cnblogs.com/helianthus33/p/10172049.html

SQL中PIVOT 使用相关推荐

  1. 关于SQL中PIVOT函数的使用方法

    文章目录 前言 一.关于PIVOT函数    1.什么是PIVOT函数 2.它能实现什么样的效果 二.使用方法 三.使用前后的效果 总结 前言 这篇文章给大家分享的是"pivot函数是什么, ...

  2. SQL中PIVOT 行列转换

    来源:http://www.studyofnet.com/news/295.html PIVOT通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列 ...

  3. SQL中PIVOT的用法

    PIVOT 提供的语法比一系列复杂的 SELECT-CASE 语句中所指定的语法更简单和更具可读性. 以下是带批注的 PIVOT 语法: SELECT <非透视的列>,[第一个透视的列] ...

  4. sql中pivot函数的使用

    1.数据的创建 create table DailyIncome(VendorId nvarchar(10), IncomeDay nvarchar(10), IncomeAmount int)--V ...

  5. python行转列_pandas.DataFrame中pivot()如何实现行转列的问题(代码)

    本篇文章给大家带来的内容是关于pandas.DataFrame中pivot()如何实现行转列的问题(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 示例: 有如下表需要进行行转 ...

  6. [MSSQL]也说SQL中显示星期几函数

    网上盛传着三个版本,分别来看下 版本1 http://bernardstudios.com/select-day-of-week-name-using-t-sql/ SELECT CASE (DATE ...

  7. 盘点SQL中最难的5件事

    告诉你从Python转换到SQL时经历的5件最难的事情. 扫码关注<Python学研大本营>,加入读者群,分享更多精彩 有15年分析经验的Josh Berry在从Python转换到SQL时 ...

  8. SQL中的动态SQL

    在介绍动态SQL前我们先看看什么是静态SQL 静态SQL 静态 SQL 语句一般用于嵌入式 SQL 应用中,在程序运行前,SQL 语句必须是确定的,例如 SQL 语句中涉及的列名和表名必须是存在的.静 ...

  9. sql中聚合函数和分组函数_学习SQL:聚合函数

    sql中聚合函数和分组函数 SQL has many cool features and aggregate functions are definitely one of these feature ...

最新文章

  1. 引用(Reference)
  2. IT部门在企业信息化中的转变
  3. linux 使用 FIO 测试磁盘的iops
  4. ubuntu 16.04 搭建 python 开发环境
  5. wordpress进阶教程(十九):创建自定义的找回密码页面
  6. ios解决button重复点击的问题
  7. 简书python_python实现简书点赞
  8. V.Replication and Sharding(创建主从数据库)
  9. ArcGIS Desktop10.2与CityEngine2012兼容问题
  10. Linux基础命令大全
  11. JUC总览,来自汪文君整理
  12. dtft频移性质_08 DTFT变换的性质
  13. JUCE学习笔记04-LookAndFeel类自定义Slider颜色
  14. 联想台式计算机内置网卡,联想台式机有没有无线网卡
  15. cesium粒子特效
  16. CSS学习笔记7PS切图与仿学成在线例子
  17. H3C-WX2510H对接OpenPortal网络准入认证计费系统实现Mac快速认证+Portal认证
  18. 注册昵称时限制 中文7个字 字母21个
  19. 【题解】Cutting Woods
  20. CSAPP HITICS 大作业 hello's P2P by zsz

热门文章

  1. JavaScript学习(二十)—DOM中常用的属性
  2. 深入理解CSS中的line-height的使用
  3. Flash写操作流程
  4. 理财产品利息可每天提取吗?
  5. 门当户对的感情真的很重要吗?
  6. 产品,是解决问题的载体
  7. 男朋友花3000元买一块电脑显卡,他是怎么想的?
  8. 在软件工程中有两件难事
  9. 分布式理论(3):Paxos Made Simple
  10. Mybatis_day4_Mybatis的延迟加载