我正在创建一个Reps委员会报告 - 下面是我的SQL声明。
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
我现在想要的是 - 让一位代表在线上发布 - 以及他们的总销售额。否则,代表将在他们所做的每笔交易之间进行分配。在Excel中进行总结时,这是一项使命。
答案 0 :(得分:0)
这是一种方法,使用上面的确切查询,然后使用聚合查询。您指定的唯一聚合是TotSales - 但希望模式变得清晰。
; -- WITH must be proceeded by a semicolon....weird but true (could be on prior stmt
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.RepCode
, SUM(SalesDetail.TotSales) As RepTotSales
FROM SalesDetail
GROUP BY SalesDetail.RepName, SalesDetail.RepCode