We help IT Professionals succeed at work.

# Months in Order

on
Medium Priority
5,991 Views
I run this:
SELECT     count(open_time)as Count,DATENAME(mm, open_time)AS Month
FROM       probsummarym1
WHERE (datediff (mm, getdate ( ) ,Open_Time) > -4)
GROUP BY   DATENAME(mm, open_time)

My return is this:
Count           Month
7498      May
7069      June
3855      July
6676      April
How can I run the month column to be in order.  April, May, June, July
Comment
Watch Question

## View Solution Only

Commented:
SELECT     count(open_time)as Count,DATENAME(mm, open_time)AS Month
FROM       probsummarym1
WHERE (datediff (mm, getdate ( ) ,Open_Time) > -4)
GROUP BY   month(open_time),DATENAME(mm, open_time)AS Month
order by  month(open_time),DATENAME(mm, open_time)AS Month

Not the solution you were looking for? Getting a personalized solution is easy.

CERTIFIED EXPERT
Top Expert 2010

Commented:
SELECT     count(open_time)as Count,DATENAME(mm, open_time)AS Month
FROM       probsummarym1
WHERE (datediff (mm, getdate ( ) ,Open_Time) > -4)
GROUP BY   DATENAME(mm, open_time)
ORDER BY CONVERT(datetime, DATENAME(mm, open_time) + ' 1, 2001')

Commented:
SELECT     count(open_time)as Count,DATENAME(mm, open_time)AS Month
FROM       probsummarym1
WHERE (datediff (mm, getdate ( ) ,Open_Time) > -4)
GROUP BY   DATENAME(mm, open_time)
ORDER BY Month(open_time)

Commented:
dbeneit - CORRECT - After removing the AS Month at the end of GROUP BY & order by it worked the first time.

matthewspatrick - ERROR - Line 5: Incorrect syntax near '1,2001'
dgmmg - ERROR - Column name 'open_time' is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
Thank you all for your efforts.
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile