SQL按月和年分组


72

我不确定在下面的SQL查询中应该写些什么来显示“日期”列,例如:“ month-year”-“ 9-2011”。

SELECT MONTH(date) + '.' + YEAR(date) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY MONTH(date), YEAR(date)

因此,我要做的是将第一列中的数据更改为显示月份和年份,而不是仅显示月份。


这个问题有些令人困惑-您是否要按日期排序?
克里斯·卡什韦尔

我不明白这一点:“我想显示第一列,因为我想以不同的方式设置第一列的格式,这样它才是唯一的”。
Stefan Steinegger'3

@JNK-Microsoft SQL @Chris-不,我不是要按日期排序,我只想将第一列的结果更改为显示月份和年份,而不是仅显示月份。
wegelagerer 2011年

1
您是否不需要先按年分组,然后按月分组?您能否显示一些示例输出?
用户未知

Answers:


163
SELECT CAST(MONTH(date) AS VARCHAR(2)) + '-' + CAST(YEAR(date) AS VARCHAR(4)) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY CAST(MONTH(date) AS VARCHAR(2)) + '-' + CAST(YEAR(date) AS VARCHAR(4))

或者如@ 40-Love所述,您可以使用前导零进行转换:

GROUP BY 
  CAST(YEAR(date) AS VARCHAR(4)) + '-' + right('00' + CAST(MONTH(date) AS VARCHAR(2)), 2) 

用这种方法按日期排序是没有用的
madd

14

我想是MS SQL,因为它看起来像MS SQL语法。

因此,您应该在分组行中输入与select ex中相同的内容:

Select MONTH(date)+'-'+YEAR(date), ....
...
...
...
group by MONTH(date)+'-'+YEAR(date)

2
MONTH(date)返回int。它需要一些cast-ing。
马丁·史密斯

true ..应该强制转换为varchar,尽管我的意思是,只要在组部分中可以包含相同的内容,就可以在选择中放入任何内容。理论上,从sql的角度来看,select语句是有效的。
Dumitrescu Bogdan

MS SQL中的no不会返回字符串,但是我写的是出于分组演示的角度。
Dumitrescu Bogdan

6

如果我理解正确。为了按要求对结果进行分组,Group By子句需要与select语句具有相同的表达式。

GROUP BY MONTH(date) + '.' + YEAR(date)

要将日期显示为“月日期”格式,请更改“。”。到'-'的完整语法将是这样的。

SELECT MONTH(date) + '-' + YEAR(date) AS Mjesec, SUM(marketingExpense) AS
SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY MONTH(date) + '.' + YEAR(date)

2
猜猜您从未尝试过运行此代码,因为它可能无法运行?
NickG '18 -4-6

6

在上面的SQL Server 2012中,我更喜欢使用format()函数,更加简化。

SELECT format(date,'MM.yyyy') AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY format(date,'MM.yyyy')

1
我不得不使用DATE_FORMAT(date, '%m.%Y')
CTS_AE,

3

您可以尝试乘以调整年份和月份,以便它们成为一个数字。根据我的测试,它的运行速度比快得多format(date,'yyyy.MM')。我更喜欢将前一年用于分类目的。从MS SQL Server Express版本12.0创建的代码。

SELECT (YEAR(Date) * 100) + MONTH(Date) AS yyyyMM
FROM [Order]
...
GROUP BY (YEAR(Date) * 100) + MONTH(Date)
ORDER BY yyyyMM

1
我只是在读这篇文章,并打算发表同样的评论。这非常有效
user1664043 '20

2
Yet another alternative: 

Select FORMAT(date,'MM.yy')
...
...
group by FORMAT(date,'MM.yy')

1

在postgresql中,我可以使用日期格式函数(to_char)编写类似的查询,并仅按日期进行分组:

SELECT to_char (datum, 'MM-YYYY') AS mjesec 
FROM test 
GROUP BY datum 
ORDER BY datum;

SQL Server当然也可以实现这种功能,不是吗?



-6

如果要保留日期时间数据类型的字段,请尝试使用以下方法:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, o.[date]), 0) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order] o
WHERE (idCustomer = 1) AND (o.[date] BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, o.[date]), 0)

也可以轻松地按小时,天,周,年分组。
我希望对某人有用,

问候!


您的陈述式开了3个方括号,结束了1个,datediff需要3个参数。这行不通。
gus27
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.