[英]pivot and unpivot a table with multi columns in tsql
我需要透視此表,如下所示: 我的當前表
我需要的是一個具有13列的表格,看起來像這樣: 所需的數據透視表
我在RazorSQL
使用RazorSQL
。 這是我產生最終表的代碼,現在需要進行透視。 :
select s5.datess, ISNULL(s5.Active_And_Good,0) AS Active_And_Good, ISNULL(s5.Inactive_And_Good,0) AS Inactive_And_Good,
ISNULL(s5.Active_And_Bad,0) AS Active_And_Bad, ISNULL(s6.Inactive_And_Bad,0) AS Inactive_And_Bad
from
(select s3.dates as datess, s3.#Active_Good as Active_And_Good, s3.#Inactive_Good as Inactive_And_Good,
s4.Active_Bad as Active_And_Bad from
(select s1.Dates as dates, s1.Active_Good as #Active_Good, s2.Inactive_Good as #Inactive_Good from
(select count(DISTINCT Customer_Id) as Active_Good, Dates
from #fact_table
where Customer_Status = 1
group by Dates) as s1
full outer join
(select count(DISTINCT Customer_Id) as Inactive_Good, Dates
from #fact_table
where Customer_Status = 2
group by Dates) as s2
on s1.Dates=s2.Dates) as s3
full outer join
(select count(DISTINCT Customer_Id) as Active_Bad, Dates
from #fact_table
where Customer_Status = 3
group by Dates) as s4
on s3.dates= s4.Dates) as s5
full outer join
(select count(DISTINCT Customer_Id) as Inactive_And_Bad, Dates
from #fact_table
where Customer_Status = 4
group by Dates) as s6
on s5.datess= s6.Dates ;
這應該工作。 在提供的表格的簡化版本上進行了測試。
Create table #test ([datess] date, [Active_AndGood] int, [Inactive_And_Good] int)
insert into #test ([datess],[Active_AndGood],[Inactive_And_Good])
values('2015-01-31',1,4) , ('2015-02-28',2,3)
select *
from #test
unpivot (value for name in ([Active_AndGood],[Inactive_And_Good])) up
pivot (max(value) for datess in ([2015-01-31],[2015-02-28])) p
產生這個:
每當需要旋轉或取消旋轉時,我都會查看這些鏈接,以使我對這些事情的工作方式記憶猶新。
https://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/
http://www.kodyaz.com/articles/t-sql-pivot-tables-in-sql-server-tutorial-with-examples.aspx
http://blog.jontav.com/post/8344518585/convert-rows-to-columns-columns-to-rows-in-sql
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/understanding-sql-server-2000-pivot/
-- Creating Test Table
CREATE TABLE Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT)
GO
-- Inserting Data into Table
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','VEG',2)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','SODA',6)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','MILK',1)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','BEER',12)
INSERT INTO Product(Cust, Product, QTY)
VALUES('FRED','MILK',3)
INSERT INTO Product(Cust, Product, QTY)
VALUES('FRED','BEER',24)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','VEG',3)
GO
-- Selecting and checking entires in table
SELECT *
FROM Product
GO
-- Pivot Table ordered by PRODUCT
SELECT PRODUCT, FRED, KATE
FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt
ORDER BY PRODUCT
GO
-- Pivot Table ordered by CUST
SELECT CUST, VEG, SODA, MILK, BEER, CHIPS
FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT (SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt
ORDER BY CUST
GO
-- Unpivot Table ordered by CUST
SELECT CUST, PRODUCT, QTY
FROM
(
SELECT CUST, VEG, SODA, MILK, BEER, CHIPS
FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT
( SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt) p
UNPIVOT
(QTY FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)
) AS Unpvt
GO
-- Clean up database
DROP TABLE Product
GO
結果集:
-- Selecting and checking entires in table
Cust Product QTY
------------------------- -------------------- -----------
KATE VEG 2
KATE SODA 6
KATE MILK 1
KATE BEER 12
FRED MILK 3
FRED BEER 24
KATE VEG 3
-- Pivot Table ordered by PRODUCT
PRODUCT FRED KATE
-------------------- ----------- -----------
BEER 24 12
MILK 3 1
SODA NULL 6
VEG NULL 5
-- Pivot Table ordered by CUST
CUST VEG SODA MILK BEER CHIPS
------------------------- ----------- ----------- ----------- ----------- -----------
FRED NULL NULL 3 24 NULL
KATE 5 6 1 12 NULL
-- Unpivot Table ordered by CUST
CUST PRODUCT QTY
------------------------- -------- -----------
FRED MILK 3
FRED BEER 24
KATE VEG 5
KATE SODA 6
KATE MILK 1
KATE BEER 12 12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.