在CASE语句和GROUP BY子句中计算保证金

时间:2015-11-06 06:07:28

标签: sql group-by case aggregate margin

我认为我在选择按分组语句聚合列的过程中遇到了错误。

我正在计算保证金百分比并使用像这样的案例陈述分组成几个月:

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)

但我不能像那样格式化我的案例陈述。你会如何计算出这种方程?

谢谢,

编辑:完整查询

 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

1 个答案:

答案 0 :(得分:0)

您可以使用子查询来获取每月当前CASE语句中的收入总和(1月如下所示)。

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]