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.
Group By Month and Year We also use YEAR() and MONTH() functions to ensure that data is grouped and ordered by month and year numbers. In the above query, if we use date_format function in group by clause, then MySQL will sort the groups alphabetically, instead of chronologically.
To select all entries from a particular month in MySQL, use the monthname() or month() function.
You can group month and year with the help of function DATE_FORMAT() in MySQL. The GROUP BY clause is also used.
SQL Server MONTH() function overviewThe MONTH() function returns an integer value which represents the month of a specified date. The MONTH() function takes an argument which can be a literal date value or an expression that can resolve to a TIME , DATE , SMALLDATETIME , DATETIME , DATETIME2 , or DATETIMEOFFSET value.
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)
You need group by MONTH(Date)
, not DATE
select MONTH(DATE) As "Month"
from DW******.SL****
Group By MONTH(DATE)
Order by MONTH(DATE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With