我有一个存储过程,而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, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits FROM [Order] WHERE (YEAR(OrderDate) = @year) GROUP BY { fn MONTHNAME(OrderDate) }, MONTH(OrderDate), YEAR(OrderDate) order by Year(orderDate),month(OrderDate)
请注意,您需要将要排序的所有字段添加到group by子句中