Order By month and year in sql with sum

前端 未结 7 2137
说谎
说谎 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:23

    If you want to order as Jan, Feb, March use

    SELECT 
      month(OrderDate) MonthName, 
      year(OrderDate) Year, 
      sum(TotalValue) Profits
    FROM         
      [Order]
    WHERE     
      month(OrderDate) = @year
    ORDER BY 
      year(OrderDate), 
      month(OrderDate)
    GROUP BY 
      year(OrderDate),
      month(OrderDate)
    
    0 讨论(0)
  • 2020-12-29 09:25

    I think you should use order by clause at the end

    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) 
    ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
    

    or

    you can do like this

    SELECT     CASE { fn MONTH(OrderDate) } 
                when 0 then 'JAN'
                when 1 then 'FEB'
                when 2 then 'MAR'
                when 3 then 'APR'
                when 4 then 'MAY'
                when 5 then 'JUN'
                when 6 then 'JUL'
                when 7 then 'AUG'
                when 8 then 'SEP'
                when 9 then 'OCT'
                when 10 then 'NOV'
                when 11 then 'DEC'
               END
          AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
        FROM         [Order]
        WHERE     (YEAR(OrderDate) = @year)
        GROUP BY { fn MONTH(OrderDate) }, YEAR(OrderDate) 
        ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-29 09:38

    Just add

    Order by max(OrderDate) 
    

    at the end.

    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)
    Order by max(OrderDate) 
    

    Now about how it works:

    If you order by month , year separately, it will go in ascending order of month in alphabetical order (April before January). If you order by order date id will be ordered based on a date value which is of course ordered by month/year.

    0 讨论(0)
  • 2020-12-29 09:38
    select 
    cast(year(appointmentdate) as char(4))+''+ 
    case when len(cast(month(appointmentdate)as char(2)))=1 
    then '0'+cast(  month(appointmentdate) as char(2) ) else cast(month(appointmentdate) as char(2))end  as apporder
    from timeslots  
    group by appointmentdate
    order by appOrder
    
    0 讨论(0)
  • 2020-12-29 09:39

    Use Following query it will work for you.

    SELECT     { fn MONTHNAME(OrderDate) } AS MonthName, datepart(MM,OrderDate)                     AS Month, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
    FROM         [Order]
    WHERE     (YEAR(OrderDate) = @year)
    GROUP BY { fn MONTHNAME(OrderDate) }, YEAR(OrderDate),     datepart(MM,**OrderDate**) 
    ORDER BY datepart(MM,**OrderDate**)
    
    0 讨论(0)
提交回复
热议问题