我有一个存储过程,select语句是:
SELECT { fn MONTHNAME(OrderDate) } AS MonthName,YEAR(OrderDate) AS Year,SUM(TotalValue) AS Profits FROM [Order] WHERE (YEAR(OrderDate) = @year) GROUP BY { fn MONTHNAME(OrderDate) },YEAR(OrderDate)
这显示了每个月的总和
但我需要按月和年订购结果,因为我的结果显示如下:
April 2013 February 2013 January 2013 June 2013 March 2013 May 2013
在这种情况下,解决方案是什么?
解决方法
试试这个:
SELECT { fn MONTHNAME(OrderDate) } AS MonthName,MONTH(OrderDate),YEAR(OrderDate) order by Year(orderDate),month(OrderDate)
请注意,您需要将您订购的任何字段添加到group by子句中