Order By month and year in sql with sum

前端 未结 7 2142
说谎
说谎 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:29

    Try this. It works perfectly. Note that you already entering a year value. So you don't need to order or group Year since you can only choose one Year at a time. So the extra code is not needed.

    SELECT { fn MONTHNAME(OrderDate) } AS MonthName, SUM(TotalValue) AS Profits
    FROM [Order]
    WHERE (YEAR(OrderDate) = @year)
    GROUP BY { fn MONTHNAME(OrderDate) }, DATEPART(M, OrderDate)
    ORDER BY DATEPART(M, OrderDate)
    

提交回复
热议问题