2017-04-12 13 views
0

に複数列を持つテーブルをアンピボット: my current table enter image description hereピボットと、私はこのようになります。このテーブルを旋回させる必要があるTSQL

私は必要なものは、次のようになります13個の列を持つテーブルである: desired pivoted table enter image description here

私はRazorSQLでtsqlを使用しています。ここで私のコードは最終的なテーブルになりました。今度はそれをピボットする必要があります。 :

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 

のResultSet:

-- 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 
関連する問題