SQL中PIVOT 使用

--语法形式:     
--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

猜你喜欢

转载自www.cnblogs.com/helianthus33/p/10172049.html