Grouping by MONTH in SQL

William picture William · Jan 11, 2013 · Viewed 12.5k times · Source

I am trying to put together a table that will take a full date, YYYY-MM-DD, pull out only the month information, and then group the months together as individual rows.

I've tried using the MONTH(DATE) command, with a Group by and Order By command, and I get the months returned for the 4 years listed on the table. I gives back 48 rows, listed 1-12, repeating 4 times. I want to get 1 return of 1-12 in order.

Here is the code I have so far.

select MONTH(DATE) As "Month"
from DW******.SL****
Group By DATE
Order by DATE 

Just started using SQL, so I apologize for the simple question.

If someone could please give me a hand. This is Db for i and I was going to use CONVERT but that does not work with our server.

Answer

Farzad picture Farzad · Jan 11, 2013

can't you use month(date) in your group by?

select MONTH(DATE) As "Month"
from DW******.SL****
Group By MONTH(DATE)
Order by MONTH(DATE)