Access,Between子句与上一年的日期

时间:2016-05-13 19:05:26

标签: sql date between access

以下查询按月计算当前年度的销售数据。我想修改查询,使其返回相同的月度销售数据,但前一年。

第二行代码中的BETWEEN子句为当前年份设置了它。有没有办法修改BETWEEN子句,以便在1月1日到12月31日之间设置它?

SELECT Format(DatePart("m",months.month_start),"00") & "/" & Year(months.month_start) 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 months.month_start and months.month_end) 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 months.month_start and months.month_end) 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 months.month_start and months.month_end) AS [Sales Margin], 

Round((((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 months.month_start and months.month_end)/(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 months.month_start and months.month_end))*100),0) & "%" AS [Profit Margin], (SELECT Round(Nz(Sum(inventory.VENDOR_ACTUAL_PRICE * deductions_inventory.quantity), 0),2) FROM INVENTORY INNER JOIN DEDUCTIONS_INVENTORY ON INVENTORY.INVENTORY_ID = DEDUCTIONS_INVENTORY.INVENTORY_ID WHERE DEDUCTIONS_INVENTORY.ENTRY_DATE between months.month_start and months.month_end) AS [Inventory Deductions], (SELECT Round(Nz(Sum(inventory.VENDOR_ACTUAL_PRICE * DEDUCTIONS_EXPENSE.quantity), 0),2) FROM INVENTORY INNER JOIN DEDUCTIONS_EXPENSE ON INVENTORY.INVENTORY_ID = DEDUCTIONS_EXPENSE.INVENTORY_ID WHERE DEDUCTIONS_EXPENSE.ENTRY_DATE between months.month_start and months.month_end) AS [Inventory Expenses],

(SELECT Round(Nz(Sum(returns.selling_price * returns.quantity), 0),2)
FROM returns inner JOIN inventory ON INVENTORY.INVENTORY_ID = returns.INVENTORY_ID
WHERE returns.return_date between months.month_start and months.month_end) AS [Sales Returns]

FROM
 (SELECT DateSerial(Year(sale_date), Month(sale_date), 1) AS month_start,
  DateAdd("d", -1, DateSerial(Year(sale_date), Month(sale_date) + 1, 1)) AS month_end

FROM SALES_RECEIPT WHERE sale_date between #1/1# And #12/31#
GROUP BY Year(sale_date), Month(sale_date))  AS months;

0 个答案:

没有答案