我正在寻找一种让ACCESS查询返回一年中每个月的每月汇总计算的方法。
现在我使用UNION
语句每月重复下面的代码,但这不是非常有效,我必须为每年创建一个单独的查询。
我最近问了一个类似的问题,关于如何获得查询返回年度汇总计算多年,并收到了两个很好的解决方案。几个月我尝试修改这些解决方案,但我无法让它工作。
我非常感谢每月汇总计算的解决方案。
SELECT DISTINCT
Format(DatePart("m",sale_date),"00") & " / " & DatePart("yyyy",sale_date) AS [Month / Year],
(SELECT Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2)
FROM SALES_RECEIPT
INNER JOIN INVENTORY ON INVENTORY.INVENTORY_ID = SALES_RECEIPT.INVENTORY_ID
WHERE SALES_RECEIPT.[SALE_DATE] Between #1/1/2014# And #1/31/2014#) AS [Gross Sales],
(SELECT Round(Nz(Sum((Nz(inventory.VENDOR_ACTUAL_PRICE,0))*sales_receipt.quantity),0),2)
FROM SALES_RECEIPT
INNER JOIN INVENTORY ON INVENTORY.INVENTORY_ID = SALES_RECEIPT.INVENTORY_ID
WHERE SALES_RECEIPT.[SALE_DATE] Between #1/1/2014# And #1/31/2014#) AS COGS,
(SELECT Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) - Nz(Sum(inventory.VENDOR_ACTUAL_PRICE * sales_receipt.quantity),0),2)
FROM INVENTORY
INNER JOIN SALES_RECEIPT ON INVENTORY.INVENTORY_ID = SALES_RECEIPT.INVENTORY_ID
WHERE SALES_RECEIPT.[SALE_DATE] Between #1/1/2014# And #1/31/2014#) AS [Sales Margin]
FROM
INVENTORY INNER JOIN SALES_RECEIPT ON INVENTORY.INVENTORY_ID = SALES_RECEIPT.INVENTORY_ID
WHERE (((SALES_RECEIPT.SAle_date) Between #1/1/2014# And #1/31/2014#))
GROUP BY
Format(DatePart("m",sale_date),"00") & " / " & DatePart("yyyy",sale_date)
答案 0 :(得分:0)
您需要进行查询以获取您的值并进行计算,然后将其包含在子查询中以进行聚合。试试这个(未经测试):
SELECT a.[Sale Month],
a.[Sale Year],
ROUND(SUM(a.[Gross Sales]),2) AS [Gross Sales],
ROUND(SUM(a.[COGS]),2) AS [COGS],
ROUND(SUM(a.[Sales Margin]),2) AS [Sales Margin],
Round((Sum(a.[Sales Margin])/Sum([Gross Sales]))/100,2) AS [Profit Margin]
FROM
(
SELECT
Format(DatePart("m",s.sale_date),"00") AS [Sale Month],
DatePart("yyyy",s.sale_date) AS [Sale Year],
Nz(s.SELLING_PRICE * s.quantity,0) AS [Gross Sales],
Nz(i.VENDOR_ACTUAL_PRICE,0)*Nz(s.quantity,0) AS [COGS],
Nz(s.SELLING_PRICE * s.quantity,0) - Nz(i.VENDOR_ACTUAL_PRICE * s.quantity,0) AS [Sales Margin]
FROM
INVENTORY i
INNER JOIN SALES_RECEIPT s ON i.INVENTORY_ID = s.INVENTORY_ID
WHERE DatePart("m",sale_date) = 1
AND DatePart("yyyy",sale_date) = 2014
) a
GROUP BY a.[Sale Month], a.[Sale Year]
ORDER BY a.[Sale Year], a.[Sale Month]
只需删除或更改WHERE
子句即可扩展分组。
以下是基于您的问题的更新。您可以看到如何修改WHERE
语句以允许更大的范围。您还可以看到[Month / Year], [Sale Month],
和[Sale Year]
列在GROUP BY
语句中的显示方式,[Month / Year]
字段中显示的SELECT
字段以及{{ 1}}和[Sale Month],
字段显示在[Sale Year]
语句中。一个用于展示,另外两个用于正确排序。
ORDER BY