Order By month and year in sql with sum

前端 未结 7 2138
说谎
说谎 2020-12-29 08:58

I have a stored procedure and the select statement is:

SELECT     { fn MONTHNAME(OrderDate) } AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profi         


        
相关标签:
7条回答
  • 2020-12-29 09:49

    Try this:

    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)
    

    Note you need to add any fields you are ordering by to the group by clause

    0 讨论(0)
提交回复
热议问题