Solved

Need SQL statement to create a list of unique month numbers (ie: integer 1 - 12) of dates found in a daterange

Posted on 2012-12-29
8
317 Views
Last Modified: 2012-12-29
I need to create a list of unique month numbers (ie: 1- 12) from a datetime field for dates in a date range, and would like to order the month numbers by date ascending

So if I had the following dates:
  - November 23, 2012
  - December 2, 2012
  - December 14, 2012
  - November 12, 2012
  - December 22, 2012
  - February 4, 2013

then I would like the output to be:
  - 11
  - 12
  - 2

The dates are all in a datetime format

I have tried both the Distinct and Group by clauses but I am struggling to find something that works.

(ie: something like 'Select distinct(month(gamedatetime)) from games order by gamedatetime asc' which now throws me an error)  

Thanks, Peter
0
Comment
Question by:pbissegger
8 Comments
 
LVL 10

Expert Comment

by:JEaston
ID: 38729728
I would have suggested going with a formula similar to what you have suggested.

Firstly, is the field a Datetime type?

Irnocially, testing this in MS SQL your formula works.  Which DBMS are you using?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 38729736
which error?
0
 

Author Comment

by:pbissegger
ID: 38729804
The error is:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

I am using an MSSQL database (probably 2005).

But I cannot change it to 'ORDER BY month(gamedatetime)' (which does not throw an error) because that gives me a numerical order from 1 - 12.... and not a series of months in order of date.

For example, if I have a hockey season the months will span from September to April (which is 9 - 4...)

Peter
0
 
LVL 10

Expert Comment

by:JEaston
ID: 38729834
The problem is you are trying to sort by the date, when the date is not in the query.

Try:  Select distinct(month(gamedatetime)) AS Months From games Order By Months asc
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 100 total points
ID: 38729851
Try following

Select distinct(month(gamedatetime)) from (Select gamedatetime From games order by gamedatetime asc)
0
 

Author Comment

by:pbissegger
ID: 38730034
Here's the results:

1. I did not think that the JEaston solution would work, and it did not. It returned a list in numerical order (ie: 1,2,3,4) even though the months do not appear in this order (ie: they are 9,10,11, 1,2,3) when I just list out the dates in ascending order from the database.

2. I thought that CodeCruiser's solution would work, but it did not. Logically, the code is correct. When I run the inner SQL statement I get a list of all the dates in the right order, but when I run the 'Select Distinct(month(gamedatetime)) from' in front of it, it reorders it into Numerical order.

Why ? It doesn't make any sense

I then tried different versions of Codecruiser's suggestion, including:

a) replacing the Distinct by a Group By
b) Putting the Months(gamedatetime) inside the inner SQL, and just having a 'Select Distinct from ' clause outside on its own
c) Same as (b) but with the Group By Clause

All of them have the inner SQL working perfectly, but using the Distinct or Group By Clauses reorders it.

Any suggestions ?
0
 
LVL 10

Accepted Solution

by:
JEaston earned 400 total points
ID: 38730052
Sorry, I missed that the output should still be month/year order.

What about something like:  Select distinct month(gamedatetime) AS Months, year(gamedatetime) AS Years From games Order By Years asc, Months asc

Downside, it outputs the 'Years' column which you do not want, but it orders correctly.  I'm sure there is a neater way - but as it's midnight here my brain is not comming up with anything better!
0
 

Author Closing Comment

by:pbissegger
ID: 38730105
Excellent solution by JEaston.

I think also CodeCruiser's solution was solid, but possibly there is a bug in MSSQL 2005.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now