2015-11-06 117 views
1

我想我在運行時遇到了一個錯誤,那就是我如何選擇通過語句來聚合我的列。計算CASE語句和GROUP BY子句中的保證金

我工作了保證金%和分組到幾個月使用CASE語句,像這樣:

SUM(CASE WHEN MONTH(DateProcessed) = 1 AND Revenus <> 0 THEN (Revenue - Cost)/Revenue ELSE 0 END AS [January]

所以我方程最終爲:SUM((Revenue - Cost)/Revenue)

,其與多個事務個月給我錯誤的值

我確定我的等式需要爲:SUM(Revenue - Cost)/SUM(Revenue)

但我無法格式化我的case語句。你如何制定出這樣的等式?

感謝,

編輯:全面查詢

SELECT 
    AR.Code AS [CustomerID] 
    ,MAX(AR.Name) AS [CustomerName] 
    ,MAX(AR.PAddress1) AS [PAddress1] 
    ,MAX(AR.PAddress2) AS [PAddress2] 
    ,MAX(AR.PAddress3) AS [PAddress3] 
    ,MAX(AR.PPostCode) AS [PostCode] 
    ,MAX(CASE 
     WHEN AR.CashAC = 1 
      THEN 'C.O.D' 
     WHEN AR.CashAC = 0 
      THEN 'Trading' 
    END) AS [Account Type (COD/Trading)] 
    ,MAX(AR.Profile) AS [Payment Profile] 
    ,MAX(CASE 
     WHEN AR.Email LIKE '%@%' 
      THEN 'Yes' 
     ELSE 'No' 
    END) AS [Email?] 
    ,MAX(Codes.Description) AS [PriceLevel] 
    ,MAX(CASE 
     WHEN AR.CashAC = 1 
      THEN RefSaleP.Name 
     WHEN AR.CashAC = 0 
      THEN RefAreaC.Description 
    END) AS [Store Assigned] 
    ,MAX(Blackwood.Spend) AS [Blackwood] 
    ,MAX(Distribution.Spend) AS [Distribution] 
    ,MAX(Glenelg.Spend) AS [Glenelg] 
    ,MAX(Millicent.Spend) AS [Millicent] 
    ,MAX(MtBarker.Spend) AS [MtBarker] 
    ,MAX(MtGambier.Spend) AS [MtGambier] 
    ,MAX(MtGambierTruss.Spend) AS [MtGambierTruss] 
    ,MAX(Naracoorte.Spend) AS [Naracoorte] 
    ,MAX(Norwood.Spend) AS [Norwood] 
    ,MAX(ISNULL(Blackwood.Spend,0) + ISNULL(Distribution.Spend,0) + ISNULL(Glenelg.Spend,0) + ISNULL(Millicent.Spend,0) + ISNULL(MtBarker.Spend,0) + ISNULL(MtGambier.Spend,0) + ISNULL(MtGambierTruss.Spend,0) + ISNULL(Naracoorte.Spend,0) + ISNULL(Norwood.Spend,0)) AS [Total] 

    /* Start Revenue Block */ 

    --2012 
    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2012 THEN StockTrans.TotalPrice - StockTrans.GST ELSE 0 END) AS [Sep 2012 Revenue] 
    ,......... 
    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2015 THEN StockTrans.TotalPrice - StockTrans.GST ELSE 0 END) AS [Sep 2015 Revenue] 

    /*End Revenue Block */ 
    /*Start Cost Block */ 

    --2012 
    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2012 THEN StockTrans.QtySupplied * StockTrans.UnitCost ELSE 0 END) AS [Sep 2012 Cost] 
    ,......... 
    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2015 THEN StockTrans.QtySupplied * StockTrans.UnitCost ELSE 0 END) AS [Sep 2015 Cost] 

    /*End Cost Block */ 
    /*Start Margin Block */ 

    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2012 THEN ((StockTrans.TotalPrice – StockTrans.GST) – (StockTrans.QtySupplied * StockTrans.UnitCost))/(StockTrans.TotalPrice – StockTrans.GST) ELSE 0 END) AS [Sep 2012 Margin] 
    ,......... 
    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2015 THEN ((StockTrans.TotalPrice – StockTrans.GST) – (StockTrans.QtySupplied * StockTrans.UnitCost))/(StockTrans.TotalPrice – StockTrans.GST) ELSE 0 END) AS [Sep 2015 Margin] 

    /*End Margin Block*/ 
    /*Start Transactions Block */ 

    --2012 
    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2012 AND StockTrans.Sequence = 1 THEN 1 ELSE 0 END) AS [Sep 2012 Trans] 
    ,......... 
    ,SUM(CASE WHEN MONTH(StockTrans.DateProcessed) = 9 AND YEAR(StockTrans.DateProcessed) = 2015 AND StockTrans.Sequence = 1 THEN 1 ELSE 0 END) AS [Sep 2015 Trans] 

    /*End Transactions Block */ 

FROM 
    AR 
    FULL OUTER JOIN Codes ON AR.PriceLevel = Codes.Code 
    FULL OUTER JOIN Codes RefAreaC ON AR.AreaCode = RefAreaC.Code 
    FULL OUTER JOIN PSUser RefSaleP ON AR.SPerson = RefSaleP.Code 
    INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 2 GROUP BY AR.Code) Store1 ON AR.Code = Store1.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 17 GROUP BY AR.Code) Store2 ON AR.Code = Store2.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 19 GROUP BY AR.Code) Store3 ON AR.Code = Store3.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 7 GROUP BY AR.Code) Store4 ON AR.Code = Store4.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 14 GROUP BY AR.Code) Store5 ON AR.Code = Store5.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 6 GROUP BY AR.Code) Store6 ON AR.Code = Store6.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 12 GROUP BY AR.Code) Store7 ON AR.Code = Store7.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 8 GROUP BY AR.Code) Store8 ON AR.Code = Store8.Code 
    FULL OUTER JOIN (SELECT AR.Code, SUM(StockTrans.TotalPrice - StockTrans.GST) AS [Spend] FROM AR INNER JOIN StockTrans ON AR.InternalKey = StockTrans.Account WHERE StockTrans.Type IN ('RCRE','RINV') AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' AND StockTrans.Branch = 9 GROUP BY AR.Code) Store9 ON AR.Code = Store9.Code 

WHERE 
    StockTrans.Type IN ('RINV','RCRE') 
    AND Codes.Type = 'PLVL' 
    AND RefAreaC.Type = 'AREA' 
    AND StockTrans.DateProcessed BETWEEN '2012-09-01 00:00:00.000' AND '2015-10-01 00:00:00.000' 
    AND StockTrans.Branch IN ('2','17','19','7','14','6','12','8','9') 

GROUP BY 
    AR.Code 
+0

請發表您的完整查詢。 –

回答

0

您可以使用子查詢來獲得收入的總和在當前的CASE聲明每個月(一月如下圖所示)。

SELECT SUM(CASE WHEN MONTH(t1.DateProcessed) = 1 AND t1.Revenue <> 0 
       THEN (t1.Revenue - t1.Cost) 
       ELSE 0 END)/
     (SELECT SUM(t2.revenue) FROM table t2 
     WHERE MONTH(t2.DateProcessed) = 1) AS [January] 
+0

t2.revenueSum不會在group by子句中列出或彙總,我還需要每個時間段都有一個連接正確嗎?即:'SELECT SUM(Revenue)AS RevenueSum FROM表WHERE日期2012年9月至2012年10月)tsept2012' – Andrew