我有一个简单的查询,可以对数字进行平均,并按年和月分解。我遇到的问题是我无法按时间顺序找到按月计算结果的方法....以下是我的结果示例;
Month Year AverageAmount
---------------------------------
April 2012 167582.1139
August 2012 206124.9323
December 2012 192481.8604
February 2012 227612.0485
January 2012 214315.2187
July 2012 195320.075
June 2012 196174.3195
March 2012 201199.9894
May 2012 190526.0571
November 2012 203441.5135
October 2012 216467.7777
September 2012 217635.9174
April 2013 206730.5333
August 2013 197296.0563
正如您在上表中看到的那样,月份按字母顺序排列......我需要的是按时间顺序排列的结果,即......
Month Year AverageAmount
----------------------------------
January 2012 214315.2187
February 2012 227612.0485
March 2012 201199.9894
April 2012 167582.1139
May 2012 190526.0571
June 2012 196174.3195
April 2012 206730.5333
July 2012 195320.075
August 2012 206124.9323
September 2012 217635.9174
October 2012 216467.7777
November 2012 203441.5135
December 2012 192481.8604
January 2013 187268.3027
February 2013 179755.0666
March 2013 200131.6533
查询:
SELECT
datename(month, col1) Month,
year(col2) Year,
avg(col3) AverageAmount
FROM
myDB
GROUP BY
datename(month, datefunded), year(datefunded)
ORDER BY
year(datefunded), datename(month, datefunded)
非常感谢任何帮助
答案 0 :(得分:2)
只需使用此ORDER BY
:
ORDER BY
YEAR(datefunded), MONTH(datefunded)
这会按照datefunded
答案 1 :(得分:0)
看一下这个答案:Convert month name to month number in SQL Server
也许这解决了你的排序?您可以使用其中一个建议将月份转换为数字,例如三月>然后你按那个排序。