[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Months in Order

Posted on 2007-07-19
4
Medium Priority
?
5,953 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
0
Comment
Question by:rhservan
4 Comments
 
LVL 9

Accepted Solution

by:
dbeneit earned 2000 total points
ID: 19527415
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
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 19527424
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')
0
 
LVL 42

Expert Comment

by:dqmq
ID: 19527557
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)
0
 

Author Comment

by:rhservan
ID: 19530663
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question