?
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
Medium Priority
?
326 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 11

Expert Comment

by:John Easton
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 70

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:John Easton
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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 400 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 11

Accepted Solution

by:
John Easton earned 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 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