我想根据同月汇总数量。我已经完成了添加它们的查询,但问题是它只添加相同日期的同一个月。这是我对八月份的查询。
SELECT
[Purchased Product],
SUM(CONVERT(INT, [Purchased Quantity])) AS [Purchased Quantity],
[Purchased Date]
FROM
StudentPurchasedHistoryTable
WHERE
DATENAME(month, [Purchased Date]) = 'August'
GROUP BY
[Purchased Product], [Purchased Date]
显示:
Purchased Product Purchased Quantity Purchased Date
Zesto 1 2015-08-03 20:03:00.000
Zesto 10 2015-08-08 00:00:00.000
Zesto 1 2015-08-09 19:47:00.000
Mineral Water 1 2015-08-09 19:48:00.000
Mineral Water 1 2015-08-09 19:49:00.000
这是我查询仅显示月份名称。
SELECT
[Purchased Product],
SUM(CONVERT(INT, [Purchased Quantity])) AS [Purchased Quantity],
DATENAME(MONTH, [Purchased Date]) AS [Purchased Date]
FROM
StudentPurchasedHistoryTable
WHERE
DATENAME(MONTH, [Purchased Date]) = 'August'
GROUP BY
[Purchased Product], [Purchased Date]
这就是它显示的内容
Purchased Product Purchased Quantity Purchased Date
Zesto 1 August
Zesto 10 August
Zesto 1 August
Mineral Water 1 August
Mineral Water 1 August
如何将购买数量与同月相加,而不管某个月的日期和时间
答案 0 :(得分:0)
如果您只选择一个月,请从group by
删除日期或按月名汇总:
SELECT [Purchased Product], sum(CONVERT(INT, [Purchased Quantity])) as [Purchased Quantity],
datename(month, [Purchased Date]) AS [Purchased Date]
from StudentPurchasedHistoryTable
WHERE DATENAME(month, [Purchased Date]) = 'August'
group by [Purchased Product], datename(month, [Purchased Date]);
我不建议在数据中已有的列之后在select
中命名表达式。它可能导致混乱。
答案 1 :(得分:0)
每年都有一个八月,所以当你想按月分组时,你实际上想要按月和年分组。 2014年8月与2015年8月混在一起会很糟糕。
试试这个:
List