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
322 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 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 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 11

Accepted Solution

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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