简体   繁体   中英

Access, DateSerial function causing inaccurate results (user inputs 2 digit month number)

The query shown below returns monthly results for the current year. In order to create a new query that only returns a single month of results based on the month number that the user inputs, I modified the last 5 lines of code that involve the DateSerial function of the query.

The problem is that the new query has slightly different results for some months. What is causing the results between the 2 queries to not match? Other than the DateSerial function in the last 5 lines, the code has not been changed.

Last 5 lines of code of new query

 FROM (SELECT DateSerial(Year(Date()),[Enter 2 digit month number],1) AS month_start, 
    DateAdd("d", -1, DateSerial(Year(Date()),[Enter 2 digit month number],31)) AS month_end 
    FROM SALES_RECEIPT 
    WHERE sale_date between DateSerial(Year(Date()), [Enter 2 digit month number], 1) and DateSerial(Year(Date()),[Enter 2 digit month number]+1, 0)
    GROUP BY Year(sale_date), Month(sale_date))  AS months;

Query that returns monthly results for the current year:

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
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],

(SELECT Round(Nz(Sum(sales_receipt.SELLING_PRICE * sales_receipt.quantity),0) ,2)
FROM SALES_RECEIPT
WHERE SALES_RECEIPT.SALE_DATE between months.month_start and months.month_end 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 between months.month_start and months.month_end 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 between months.month_start and months.month_end and SALES_RECEIPT.SALES_TAX_EXEMPT="No") AS [Total Sales Tax],

(Select Round(Nz(Sum(Shipping_CHARGES.Shipping_COST),0),2)
FROM SHIPPING_CHARGES
WHERE SHIPPING_CHARGES.ENTRY_DATE between months.month_start and months.month_end ) AS [Total Shipping Charges]

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;

The following is a corrected version of the DateSerial function code that will return accurate results.

FROM (SELECT DateSerial(Year(date()), [Enter 2 digit month number], 1) AS month_start,
DateAdd("d", -1,  DateSerial(Year(date()), [Enter 2 digit month number] + 1, 1)) AS month_end 
FROM SALES_RECEIPT
WHERE sale_date between DateSerial(Year(Date()), [Enter 2 digit month number], 1) and DateSerial(Year(Date()), [Enter 2 digit month number]+1, 0) 
GROUP BY Year(sale_date), Month(sale_date))  AS months;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM