2017-04-12 170 views
0

多列的表,我需要轉動這個表看起來像這樣: my current table enter image description here透視和逆透視與TSQL

我需要的是有13列的表,看起來像這樣: desired pivoted table enter image description here我使用tsql 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 ; 

回答

1

這應該有效。在您提供的表格的縮減版本上進行測試。

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 

產生以下:

enter image description here

+0

非常感謝。它確實工作:) –

+0

沒問題。 〜:O) – GandRalph

0

每當我需要樞軸或逆透視,我來看看這些鏈接刷新我的這些事情是如何工作的內存。

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