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
314 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
Comment Utility
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
Comment Utility
which error?
0
 

Author Comment

by:pbissegger
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 100 total points
Comment Utility
Try following

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

Author Comment

by:pbissegger
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent solution by JEaston.

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

10 Experts available now in Live!

Get 1:1 Help Now