I would like to calculate monthly aggregate Sums with Distinct values. I have an INTERNET_SALES_RECEIPT table that has a record for each item sold with a foreign key to the INTERNET_SHIPPING_CHARGES table.
The items that are shipped in the same box have the same foreign key from the INTERNET_SHIPPING_CHARGES table.
The following code runs but the query cannot find the values for months.month_start and months.month_end causing the query to issue a prompt and get hung up.
SELECT Format(DatePart("m", months.month_start), "00") & "/" & Year(months.month_start) AS [Month/Year],
(SELECT Round(Nz(Sum(internet_shipping_charges.shipping_collected), 0), 2)
FROM
(SELECT DISTINCT internet_shipping_charges.shipping_collected
FROM internet_shipping_charges
INNER JOIN INTERNET_SALES_RECEIPT ON INTERNET_SALES_RECEIPT.INTERNET_SHIPPING_ID = INTERNET_SHIPPING_CHARGES.INTERNET_SHIPPING_ID
WHERE internet_sales_receipt.SALE_DATE BETWEEN months.month_start AND months.month_end )
) AS [Total Shipping Collected],
(SELECT Round(Nz(Sum(internet_shipping_charges.shipping_actual_cost), 0), 2)
FROM
(SELECT DISTINCT internet_shipping_charges.shipping_actual_cost
FROM internet_shipping_charges
INNER JOIN INTERNET_SALES_RECEIPT ON INTERNET_SALES_RECEIPT.INTERNET_SHIPPING_ID = INTERNET_SHIPPING_CHARGES.INTERNET_SHIPPING_ID
WHERE internet_sales_receipt.SALE_DATE BETWEEN months.month_start AND months.month_end )
) AS [Total Shipping Actual Cost],
(SELECT Round(Nz(Sum(internet_shipping_charges.shipping_collected), 0) - Nz(Sum(internet_shipping_charges.shipping_actual_cost), 0), 2)
FROM
(SELECT DISTINCT internet_shipping_charges.shipping_collected, internet_shipping_charges.shipping_actual_cost
FROM internet_shipping_charges
INNER JOIN INTERNET_SALES_RECEIPT ON INTERNET_SALES_RECEIPT.INTERNET_SHIPPING_ID = INTERNET_SHIPPING_CHARGES.INTERNET_SHIPPING_ID
WHERE internet_sales_receipt.[SALE_DATE] BETWEEN months.month_start AND months.month_end)
) AS [Shipping Gain/Loss]
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 internet_sales_receipt
WHERE sale_date BETWEEN DateAdd("yyyy", [Enter last two digits of year], #1/1/2000#) AND DateAdd("yyyy", [Enter last two digits of year], #12/31/2000#)
GROUP BY Year(sale_date), Month(sale_date)
) AS months;
I tried adding various forms of the DatePart() function to the subqueries but always got the same error of missing values for months.month_start and months.month_end.
SELECT Format(DatePart("m", months.month_start), "00") & "/" & Year(months.month_start) AS [Month/Year],
(SELECT Round(Nz(sum(internet_shipping_charges.shipping_collected), 0), 2)
FROM
(SELECT DISTINCT internet_shipping_charges.shipping_collected, internet_sales_receipt.SALE_DATE
FROM internet_shipping_charges
INNER JOIN INTERNET_SALES_RECEIPT ON INTERNET_SALES_RECEIPT.INTERNET_SHIPPING_ID = INTERNET_SHIPPING_CHARGES.INTERNET_SHIPPING_ID)
WHERE internet_sales_receipt.SALE_DATE BETWEEN months.month_start AND months.month_end
) AS [Total Shipping Collected],
(SELECT Round(Nz(sum(internet_shipping_charges.shipping_actual_cost), 0), 2)
FROM
(SELECT DISTINCT internet_shipping_charges.shipping_actual_cost, internet_sales_receipt.SALE_DATE
FROM internet_shipping_charges
INNER JOIN INTERNET_SALES_RECEIPT ON INTERNET_SALES_RECEIPT.INTERNET_SHIPPING_ID = INTERNET_SHIPPING_CHARGES.INTERNET_SHIPPING_ID)
WHERE internet_sales_receipt.SALE_DATE BETWEEN months.month_start AND months.month_end
) AS [Total Shipping Actual Cost],
(SELECT Round(Nz(sum(internet_shipping_charges.shipping_collected), 0) - Nz(sum(internet_shipping_charges.shipping_actual_cost), 0), 2)
FROM
(SELECT DISTINCT internet_shipping_charges.shipping_collected, internet_shipping_charges.shipping_actual_cost, internet_sales_receipt.SALE_DATE
FROM internet_shipping_charges
INNER JOIN INTERNET_SALES_RECEIPT ON INTERNET_SALES_RECEIPT.INTERNET_SHIPPING_ID = INTERNET_SHIPPING_CHARGES.INTERNET_SHIPPING_ID)
WHERE internet_sales_receipt.[SALE_DATE] BETWEEN months.month_start AND months.month_end
) AS [Shipping Gain/Loss]
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 internet_sales_receipt
WHERE sale_date BETWEEN DateAdd("yyyy", [Enter last two digits of year], #1/1/2000#) AND DateAdd("yyyy", [Enter last two digits of year], #12/31/2000#)
GROUP BY Year(sale_date), Month(sale_date)
) AS months;