2017-04-12 183 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