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
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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 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
 
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

Industry Leaders: 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!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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