Question: I have a stored procedure and the select statement is:

```
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)

```

this shows the sum total for every month 
But I need to order the result by month and year coz my result shown like:

```
April 2013 
February 2013 
January 2013 
June 2013 
March 2013 
May 2013 

```

What is the solution in such a case?

Answer: 
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)

```
```
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

```