使用多个WHERE子句和GROUP BY销售人员访问SQL,聚合总和

时间:2016-05-09 18:24:21

标签: sql ms-access group-by subquery

以下查询正确地总结了每日"销售总额","佣金"以及"销售成本(COGS)"为每个销售人员。

问题出在" Taxable Sales"," Tax Free Sales"和" Sales Tax" sums因为需要WHERE子句。带有WHERE子句的子查询返回每个销售人员合并的所有销售人员的总和。

我需要修改查询,以便为每个销售人员返回一个单独的值。

SELECT Format(DatePart("m",sale_date),"00") & "/" & Format(DatePart("d",sale_date),"00") & "/" & DatePart("yyyy",sale_date) AS [Date],

SALES_REP.rep_Name AS [Sales Person], 

Round(Nz(Sum(sales_receipt.SELLING_PRICE*sales_receipt.quantity),0),2)  AS [Sales Total], 

Sum((Nz(SALES_RECEIPT.SELLING_PRICE,0)*Nz(SALES_RECEIPT.quantity,0))*(Nz(SALES_RECEIPT.commission_percent,100)*0.001)) AS [Commission],

(SELECT Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2) 
FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE = Date() and SALES_RECEIPT.SALES_TAX_EXEMPT="No") AS [Taxable Sales],

(SELECT Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2) 
FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE = Date() and SALES_RECEIPT.SALES_TAX_EXEMPT="Yes") AS [Tax Free Sales],

(SELECT Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2) * .05
FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE = Date() and SALES_RECEIPT.SALES_TAX_EXEMPT="No") AS [Sales Tax],

Round(Nz(Sum((Nz(inventory.VENDOR_ACTUAL_PRICE,0))*sales_receipt.quantity),0),2) AS COGS

FROM (SALES_RECEIPT
INNER JOIN SALES_REP ON SALES_RECEIPT.REP_ID = SALES_REP.REP_ID)
LEFT JOIN INVENTORY ON INVENTORY.INVENTORY_ID = SALES_RECEIPT.INVENTORY_ID

WHERE SALES_RECEIPT.SALE_DATE=Date()
GROUP BY Format(DatePart("m",sale_date),"00") & "/" & Format(DatePart("d",sale_date),"00") & "/" & DatePart("yyyy",sale_date), SALES_REP.rep_Name;

以下代码是根据我收到的答案更新和更正的版本。

SELECT Format(DatePart("m",sale_date),"00") & "/" & Format(DatePart("d",sale_date),"00") & "/" & DatePart("yyyy",sale_date) AS [Date],

SALES_REP.rep_Name AS [Sales Person], 

Round(Nz(Sum(sales_receipt.SELLING_PRICE*sales_receipt.quantity),0),2)  AS [Sales Total], 

Sum((Nz(SALES_RECEIPT.SELLING_PRICE,0)*Nz(SALES_RECEIPT.quantity,0))*(Nz(SALES_RECEIPT.commission_percent,100)*0.001)) AS [Commission],

tsales.sum_taxable_sales AS [Taxable Sales],

tfsales.sum_tax_free_sales AS [Tax Free Sales],

tsales_tax.sum_sales_tax AS [Sales Tax],

Round(Nz(Sum((Nz(inventory.VENDOR_ACTUAL_PRICE,0))*sales_receipt.quantity),0),2) AS COGS

FROM ((((SALES_RECEIPT
INNER JOIN SALES_REP ON SALES_RECEIPT.REP_ID = SALES_REP.REP_ID)
LEFT JOIN INVENTORY ON INVENTORY.INVENTORY_ID = SALES_RECEIPT.INVENTORY_ID)

LEFT JOIN 

(SELECT SALES_RECEIPT.rep_ID, Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2) as sum_taxable_sales

FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE = Date() and SALES_RECEIPT.SALES_TAX_EXEMPT="No"
GROUP BY SALES_RECEIPT.rep_ID) 
tsales ON tsales.rep_ID = SALES_REP.rep_ID)

LEFT JOIN 

(SELECT SALES_RECEIPT.rep_ID, Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2) as sum_tax_free_sales
FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE = Date() and SALES_RECEIPT.SALES_TAX_EXEMPT="Yes"
GROUP BY SALES_RECEIPT.rep_ID) tfsales
 ON tfsales.rep_ID = SALES_REP.rep_ID)

LEFT JOIN

(SELECT SALES_RECEIPT.rep_ID, Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2)* .05 as sum_sales_tax

FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE = Date() and SALES_RECEIPT.SALES_TAX_EXEMPT="No"
GROUP BY SALES_RECEIPT.rep_ID) 
tsales_tax ON tsales_tax.rep_ID = SALES_REP.rep_ID

WHERE SALES_RECEIPT.SALE_DATE=Date()

GROUP BY Format(DatePart("m",sale_date),"00") & "/" & Format(DatePart("d",sale_date),"00") & "/" & DatePart("yyyy",sale_date)
, SALES_REP.rep_Name
, tsales.sum_taxable_sales
, tfsales.sum_tax_free_sales
, tsales_tax.sum_sales_tax
;

1 个答案:

答案 0 :(得分:0)

您可以使用子查询正确分组记录,并加入销售人员。我在查询中假设rep_name是键,否则在JOIN ON中替换它。

SELECT Format(DatePart("m",sale_date),"00") & "/" & Format(DatePart("d",sale_date),"00") & "/" & DatePart("yyyy",sale_date) AS [Date],

SALES_REP.rep_Name AS [Sales Person], 

Round(Nz(Sum(sales_receipt.SELLING_PRICE*sales_receipt.quantity),0),2)  AS [Sales Total], 

Sum((Nz(SALES_RECEIPT.SELLING_PRICE,0)*Nz(SALES_RECEIPT.quantity,0))*(Nz(SALES_RECEIPT.commission_percent,100)*0.001)) AS [Commission],

tsales.sum_taxable_sales AS [Taxable Sales],

Round(Nz(Sum((Nz(inventory.VENDOR_ACTUAL_PRICE,0))*sales_receipt.quantity),0),2) AS COGS

FROM ((SALES_RECEIPT
INNER JOIN SALES_REP ON SALES_RECEIPT.REP_ID = SALES_REP.REP_ID)
LEFT JOIN INVENTORY ON INVENTORY.INVENTORY_ID = SALES_RECEIPT.INVENTORY_ID)

LEFT JOIN (SELECT SALES_RECEIPT.rep_ID, Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2) as sum_taxable_sales

FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE = Date() and SALES_RECEIPT.SALES_TAX_EXEMPT="No"
GROUP BY SALES_RECEIPT.rep_ID) tsales ON tsales.rep_ID = SALES_REP.rep_ID

WHERE SALES_RECEIPT.SALE_DATE=Date()


GROUP BY Format(DatePart("m",sale_date),"00") & "/" & Format(DatePart("d",sale_date),"00") & "/" & DatePart("yyyy",sale_date), SALES_REP.rep_Name;

简化的语法是:

SELECT a.id, b.value
FROM a
JOIN (SELECT b.id, SUM(b.value) as value FROM b) b ON a.id = b.id