我想查询SUMS一组月份(即5月 - 8月)每个产品ID的总销售数量
我想:
+------------+-------------------------+-------+--------------+-----------------+
| PRODUCT_ID | ITEM | PRICE | GROSS_MARGIN | Sum Of QUANTITY |
+------------+-------------------------+-------+--------------+-----------------+
| 100-10 | ALVE laptop table | 89 | 56% | 23323 |
| 100-15 | FREDRIK desk | 129 | 44% | 9614 |
| 100-20 | GALANT conference table | 369 | 32% | 10272 |
+------------+-------------------------+-------+--------------+-----------------+
但是我无法设置查询来执行此操作,因为我很难访问(真的是我第一次玩弄它)。
这是我当前的查询吐出的内容:
+------------+--------------------------+---------+--------------+--------------------+----------+
| PRODUCT_ID | ITEM | PRICE | GROSS_MARGIN | TRANSDATE By Month | QUANTITY |
+------------+--------------------------+---------+--------------+--------------------+----------+
| 100-10 | ALVE laptop table | $89.00 | 56.00% | August 2011 | 9679 |
| 100-10 | ALVE laptop table | $89.00 | 56.00% | July 2011 | 9436 |
| 100-10 | ALVE laptop table | $89.00 | 56.00% | June 2011 | 3222 |
| 100-10 | ALVE laptop table | $89.00 | 56.00% | May 2011 | 986 |
| 100-15 | FREDRIK desk | $129.00 | 44.00% | August 2011 | 3150 |
| 100-15 | FREDRIK desk | $129.00 | 44.00% | July 2011 | 2695 |
| 100-15 | FREDRIK desk | $129.00 | 44.00% | June 2011 | 3769 |
| 100-20 | GALANT conference table | $369.00 | 32.00% | August 2011 | 3814 |
| 100-20 | GALANT conference table | $369.00 | 32.00% | July 2011 | 4977 |
| 100-20 | GALANT conference table | $369.00 | 32.00% | June 2011 | 225 |
| 100-20 | GALANT conference table | $369.00 | 32.00% | May 2011 | 1256 |
+------------+--------------------------+---------+--------------+--------------------+----------+
基本上,我想要一行来总结那4个月(5月到8月,包括每月的每一天)的项目的所有销售额。如有必要,请忽略其他列(价格和保证金)。
任何帮助将不胜感激,我需要在星期五之前完成! 已经玩了好几个小时,但每次我走得更近,我似乎都会打破一些东西。 :'(
这是我当前的SQL语法/代码(抱歉,不知道正确的术语)。
SELECT DISTINCTROW
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM,
PRODUCTS.PRICE,
PRODUCTS.GROSS_MARGIN,
Format$([SALES].[TRANSDATE],'mmmm yyyy') AS [TRANSDATE By Month],
Sum(SALES.QUANTITY) AS [Sum Of QUANTITY]
FROM PRODUCTS INNER JOIN
SALES ON PRODUCTS.[PRODUCT_ID] = SALES.[PRODUCT_ID]
GROUP BY
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM,
PRODUCTS.PRICE,
PRODUCTS.GROSS_MARGIN,
Format$([SALES].[TRANSDATE],'mmmm yyyy'), Year([SALES].[TRANSDATE])*12+DatePart('m',[SALES].[TRANSDATE])-1;
这实际上目前显示所有月份和年份的所有数据(我有2年所有产品的数据)。我只想要在一年(2011年5月到8月)的四个月中每个产品ID的总销售额。请帮助!
答案 0 :(得分:0)
由于这是Access,因此SQL Fiddle只能是一个开始。
您需要正确“引用”日期表达式:
SELECT
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM,
Sum(SALES.QUANTITY) AS [Sum Of QUANTITY]
FROM
PRODUCTS INNER JOIN
SALES ON PRODUCTS.[PRODUCT_ID] = SALES.[PRODUCT_ID]
WHERE
[SALES].[TRANSDATE] BETWEEN #2015-05-01# AND #2015-08-31#
GROUP BY
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM
更正2015-07-23
我非常抱歉您错过了您的原始数据有2011年而不是2011年的日期。我特此发布更正后的SQL供您测试:
SELECT
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM,
Sum(SALES.QUANTITY) AS [Sum Of QUANTITY]
FROM
PRODUCTS INNER JOIN
SALES ON PRODUCTS.[PRODUCT_ID] = SALES.[PRODUCT_ID]
WHERE
[SALES].[TRANSDATE] BETWEEN #2011-05-01# AND #2011-08-31#
GROUP BY
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM
这将从Access数据库生成输出。但是,与SQL Fiddler一起使用时,它将有所不同:
SELECT
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM,
Sum(SALES.QUANTITY) AS [Sum Of QUANTITY]
FROM
PRODUCTS INNER JOIN
SALES ON PRODUCTS.[PRODUCT_ID] = SALES.[PRODUCT_ID]
WHERE
[SALES].[TRANSDATE] BETWEEN '2011-05-01' AND '2011-08-31'
GROUP BY
PRODUCTS.PRODUCT_ID,
PRODUCTS.ITEM
很抱歉给您带来不便。