需要所有销售代表在一条线上

时间:2015-07-14 07:38:24

标签: sql sql-server tsql group-by

下面我有以下SQL语句:

WITH SalesDetail as (
SELECT
PostAR.TxDate
 ,PostAR.AccountLink
 ,PostST.AccountLink AS StkLnk
 ,PostST.Quantity AS QtySold
 ,PostST.cAuditNumber
 ,(PostST.Credit-PostST.Debit)-(PostST.Quantity*PostST.Cost) AS Profit
 ,(((PostST.Credit-PostST.Debit)-(PostST.Quantity*PostST.Cost)))/((((PostST.Credit-PostST.Debit))))*100 AS GrossProfitPercent
 ,(PostST.Quantity*PostST.Cost) AS Cost
 ,(PostST.Credit-PostST.Debit) AS TotSales
 ,(((PostST.Credit-PostST.Debit)-(PostST.Quantity*PostST.Cost))) / ((((PostST.Quantity*PostST.Cost)+(0.00000000000001))))*100 AS MarkUpPercent
 ,concat(StkItem.Code, ' - ' ,StkItem.Description_1) AS StkItemCode
 ,SalesRep.Code AS RepCode
 ,SalesRep.Name AS RepName
 ,Client.Account CustID
 ,Client.Name AS CustName

 ,CASE 
         WHEN ((((PostST.Credit+PostST.Debit)-(PostST.Cost*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) > 0 AND ((((PostST.Credit+PostST.Debit)-((PostST.Cost+0.0000001)*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) < 25  THEN 2
         WHEN ((((PostST.Credit+PostST.Debit)-(PostST.Cost*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) >= 25 AND ((((PostST.Credit+PostST.Debit)-((PostST.Cost+0.0000001)*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) < 35  THEN 2.5
         WHEN ((((PostST.Credit+PostST.Debit)-(PostST.Cost*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) >= 35 AND ((((PostST.Credit+PostST.Debit)-((PostST.Cost+0.0000001)*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) < 45  THEN 3
         WHEN ((((PostST.Credit+PostST.Debit)-(PostST.Cost*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) >= 45 AND ((((PostST.Credit+PostST.Debit)-((PostST.Cost+0.0000001)*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) < 55  THEN 3.5
         WHEN ((((PostST.Credit+PostST.Debit)-(PostST.Cost*PostST.Quantity))/((PostST.Cost+0.0000001)*PostST.Quantity))*100) >= 55 THEN 4
         ELSE 0
END AS CommPayablePercent

FROM PostAR 
 INNER JOIN PostST
ON PostST.cAuditNumber = PostAR.cAuditNumber
 INNER JOIN StkItem
ON StkItem.StockLink = PostST.AccountLink
 INNER JOIN SalesRep
ON SalesRep.idSalesRep = PostAR.RepID
 INNER JOIN Client
ON Client.DCLink = PostAR.AccountLink 
)
    SELECT  SalesDetail.RepName
    ,   (((SalesDetail.CommPayablePercent)/100)) * ((SalesDetail.TotSales))      As RepTotComm
    ,   SUM(SalesDetail.TotSales) AS TotalSales 

   FROM SalesDetail
   GROUP BY SalesDetail.RepName, SalesDetail.TotSales,       SalesDetail.CommPayablePercent
   ORDER BY SalesDetail.RepName

然后我得到以下结果:

enter image description here

正如您所看到的,它将TotSales与我已经确定的CASE公式相乘。那样就好。但是,我不希望每一行都有代表。我只需要代表名称,然后是应付的佣金总额。

例如:

灰色接力 - 5000美元 - 6000美元

而不是:

 GRAY MEIRING
 GRAY MEIRING 
 GRAY MEIRING etc etc... 

问题来自我的SUM函数还是我的GROUP BY函数?

非常感谢! :)

1 个答案:

答案 0 :(得分:2)

你应该只使用GROUP BY SalesDetail.RepName来实现它,你可以添加子查询,尝试类似的东西:

................
SELECT  RepName,
        SUM(RepTotComm),
        SUM(TotalSales)
FROM (
    SELECT  SalesDetail.RepName
    ,   (((SalesDetail.CommPayablePercent)/100)) * ((SalesDetail.TotSales))      As RepTotComm
    ,   SUM(SalesDetail.TotSales) AS TotalSales 

   FROM SalesDetail
   GROUP BY SalesDetail.RepName, SalesDetail.TotSales, SalesDetail.CommPayablePercent      
     ) x
GROUP BY RepName
ORDER BY RepName