使用多个 WHERE 子句和 GROUP BY 销售人员访问 SQL、聚合总和
Posted
技术标签:
【中文标题】使用多个 WHERE 子句和 GROUP BY 销售人员访问 SQL、聚合总和【英文标题】:Access SQL, Aggregate Sums with multiple WHERE clauses and GROUP BY sales person 【发布时间】:2016-05-09 18:24:21 【问题描述】:以下查询正确汇总了每个销售人员的每日“销售总额”、“佣金”和“销售成本 (COGS)”。
问题在于“Taxable Sales”、“Tax Free Sales”和“Sales Tax”总和,因为需要 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】:您可以使用子查询对记录进行正确分组,并加入销售人员。我在查询中假设 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
【讨论】:
Rep_id 实际上是关键,我在 JOIN ON 中更改了它。我必须根据收到的初始错误消息为 JOIN 语句添加一些括号。我在我发布的原始代码下方发布了修改后的代码,并附上了我现在收到的错误消息。我还没有尝试添加其他结果,因为我无法让它工作。 @CodeMechanik 我已根据您的反馈更新了我的查询。您需要更改所选 ID,并将 Group By 更改为要加入的正确列。 这解决了问题,我在添加其他结果时没有问题。谢谢!以上是关于使用多个 WHERE 子句和 GROUP BY 销售人员访问 SQL、聚合总和的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 where 和 Group by 返回多个总和结果
不能在 Group by/Order by/Where/ON 子句中使用 Group 或 Aggregate 函数(min()、max()、sum()、count()、...等)