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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM