2015-09-01 52 views
1

,以下是我的SQL Server的查詢:多支點在SQL Server

 SELECT t.type , 
     t.fmsales , 
     t.indussales 
FROM (SELECT CASE WHEN (GROUPING(mti.type) = 1) THEN 'ALL' 
         ELSE ISNULL(mti.Type, 'UNKNOWN') 
        END AS type , 
        SUM(ISNULL(mti.SALES_THIS + mti.SWITCHIN_THIS, 0)) fmsales , 
        SUM(ISNULL(mti.SALES_ALL + mti.SWITCHIN_ALL, 0)) indussales 

      FROM  dbo.IMonthly_trigger_Industry mti , 
        dbo.RMMappingDb rm , 
        dbo.EmployeeDB ed 
      WHERE  ed.EmployeeCode = 1217 
        AND rm.BranchName = mti.Branch 
        AND rm.ARNCode = mti.BROKER 
        AND rm.EmployeeCode = ed.EmployeeCode 
        AND mti.BROKER_NAME LIKE '%Pvt Limited%' 
      GROUP BY mti.Type 
        WITH CUBE 
     ) t 
WHERE t.type IS NOT NULL 
GROUP BY t.type , 
     t.fmsales , 
     t.indussales; 

和這樣做的結果是這樣的:

enter image description here

我要轉這個結果集,以便行成爲列和列成爲行。我早些時候使用了pivot方法,我只有一列來完成聚合,但在這裏它是不同的。有人能幫助我實現這一目標嗎?

結果應該是這樣的:

enter image description here

我怎樣才能做到這一點?我嘗試使用數據透視表,因爲數據透視表允許只有一列的聚合我無法得到預期的結果。任何幫助深表感謝。提前致謝。

回答

1

可以使用的PIVOT組合做到這一點,UNPIVOT象下面這樣:

select * 
from 
    (
     select * from t 
) s 
unpivot 
    (
    val for product in ([fmsales],[indussales]) 
)up 
pivot 
    (
    max(val) for type in ([ALL],[CASH],[DEBIT],[EQUITY]) 
    )p 

演示SQL小提琴鏈接:http://sqlfiddle.com/#!6/381f7/4

說明:

首先,我們使用UNPIVOT得到fmsalesindussales列名作爲行數據,然後PIVOT通過type col UMN。 見UNPIVOT這裏中間結果:

http://sqlfiddle.com/#!6/381f7/5

0

您可以使用UNION ALL先逆透視數據,然後有條件聚集做旋轉:

WITH Cte AS(
    --Your original query here 
    SELECT 'ALL' AS type, 0 AS fmsales, 82.1 AS indussales UNION ALL 
    SELECT 'CASH' AS type, 0 AS fmsales, 0 AS indussales UNION ALL 
    SELECT 'DEBT' AS type, 0 AS fmsales, 62 AS indussales UNION ALL 
    SELECT 'EQUITY' AS type, 0 AS fmsales, 20.1 AS indussales 
), 
CteUnpivot AS(
    SELECT 'fmsales' AS products, 'ALL' AS type, fmsales AS val FROM Cte WHERE type = 'ALL' UNION ALL 
    SELECT 'fmsales' AS products, 'CASH' AS type, fmsales AS val FROM Cte WHERE type = 'CASH' UNION ALL 
    SELECT 'fmsales' AS products, 'DEBT' AS type, fmsales AS val FROM Cte WHERE type = 'DEBT' UNION ALL 
    SELECT 'fmsales' AS products, 'EQUITY' AS type, fmsales AS val FROM Cte WHERE type = 'EQUITY'UNION ALL 
    SELECT 'indussales' AS products, 'ALL' AS type, indussales AS val FROM Cte WHERE type = 'ALL' UNION ALL 
    SELECT 'indussales' AS products, 'CASH' AS type, indussales AS val FROM Cte WHERE type = 'CASH' UNION ALL 
    SELECT 'indussales' AS products, 'DEBT' AS type, indussales AS val FROM Cte WHERE type = 'DEBT' UNION ALL 
    SELECT 'indussales' AS products, 'EQUITY' AS type, indussales AS val FROM Cte WHERE type = 'EQUITY' 
) 
SELECT 
    [ALL] = SUM(CASE WHEN type = 'ALL' THEN val ELSE 0 END), 
    CASH = SUM(CASE WHEN type = 'CASH' THEN val ELSE 0 END), 
    DEBT = SUM(CASE WHEN type = 'DEBT' THEN val ELSE 0 END), 
    EQUITY = SUM(CASE WHEN type = 'EQUITY' THEN val ELSE 0 END) 
FROM CteUnpivot 
GROUP BY products 
1

您可以通過標準的把戲時,做到這一點首先unpivoting數據然後pivoting結果:

DECLARE @t TABLE(type NVARCHAR(MAX), fmsales MONEY, indussales MONEY) 

INSERT INTO @t VALUES 
('ALL', 0, 82.1), 
('CASH', 0, 0), 
('DEBT', 0, 62), 
('EQUITY', 0, 20.1) 


SELECT * FROM @t 
UNPIVOT(a FOR products IN([fmsales],[indussales]))u 
PIVOT (MAX(a) FOR type IN([ALL],[CASH],[DEBT],[EQUITY]))p 

輸出:

products ALL  CASH DEBT EQUITY 
fmsales  0.00 0.00 0.00 0.00 
indussales 82.10 0.00 62.00 20.10 

爲了這適用於您查詢,您可以使用common table expressions(CTE),如:

;WITH cte AS(
      SELECT t.type , 
        t.fmsales , 
        t.indussales 
      FROM (SELECT CASE WHEN (GROUPING(mti.type) = 1) THEN 'ALL' 
            ELSE ISNULL(mti.Type, 'UNKNOWN') 
           END AS type , 
           SUM(ISNULL(mti.SALES_THIS + mti.SWITCHIN_THIS, 0)) fmsales , 
           SUM(ISNULL(mti.SALES_ALL + mti.SWITCHIN_ALL, 0)) indussales 
         FROM  dbo.IMonthly_trigger_Industry mti , 
           dbo.RMMappingDb rm , 
           dbo.EmployeeDB ed 
         WHERE  ed.EmployeeCode = 1217 
           AND rm.BranchName = mti.Branch 
           AND rm.ARNCode = mti.BROKER 
           AND rm.EmployeeCode = ed.EmployeeCode 
           AND mti.BROKER_NAME LIKE '%Pvt Limited%' 
         GROUP BY mti.Type 
           WITH CUBE 
        ) t 
      WHERE t.type IS NOT NULL 
      GROUP BY t.type , 
        t.fmsales , 
        t.indussales 
     ) 
SELECT * FROM cte 
UNPIVOT(a FOR products IN([fmsales],[indussales]))u 
PIVOT (MAX(a) FOR type IN([ALL],[CASH],[DEBT],[EQUITY]))p 
2

,我們可以做到這一點的交叉運用,但它的晚

DECLARE @t TABLE(type NVARCHAR(MAX), fmsales MONEY, indussales MONEY) 

    INSERT INTO @t VALUES 
    ('ALL', 0, 82.1), 
    ('CASH', 0, 0), 
    ('DEBT', 0, 62), 
    ('EQUITY', 0, 20.1) 

    select col As Products,[ALL],[Cash],[Debt],[Equity], 
    from (select type,col,val from @t 

    CROSS APPLY (values(fmsales, 'fmsales'),(indussales, 'indussales'))CS(col,val))K 

    PIVOT(MAX(val) 
    FOR TYPE IN 
    ([ALL],[Cash],[Debt],[Equity]))P