We help IT Professionals succeed at work.

Months in Order

rhservan
rhservan asked
on
Medium Priority
5,991 Views
Last Modified: 2008-01-09
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

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.

Ask the Experts
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)

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

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

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.