ACCESS SQL,每月DISTINCT汇总总和

时间:2019-01-09 01:07:18

标签: subquery aggregate distinct access datepart

我想计算具有不同值的每月总和。我有一个INTERNET_SALES_RECEIPT表,该表记录了每个出售的商品,并带有指向INTERNET_SHIPPING_CHARGES表的外键。

在同一盒子中运送的物品具有来自INTERNET_SHIPPING_CHARGES表的相同外键。

以下代码运行,但是查询找不到 months.month_start 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
    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;

我尝试将各种形式的DatePart()函数添加到子查询中,但在 months.month_start months.month_end 中始终会遇到缺少值的错误。 / p>

1 个答案:

答案 0 :(得分:0)

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;