I would like add difference between group by year(datetime),month(datetime) and groupb by extract(year_month from datetime). here is the query i tried with these two cases.
select year(datetimecol) as Year,monthname(datetimecol) as Month from table
group by year(datetimecol) and month(datetimecol) order by year(datetimecol) desc;
result:
Year, Month
2020, May
select year( datetimecol) as Year,monthname(datetimecol) as Month from table GROUP BY EXTRACT(YEAR_MONTH FROM datetimecol) order by year(datetimecol) desc,month(datetimecol) asc;
result:
Year, Month
2021, January 2021, February 2021, March 2021, April 2021, May 2020, May 2020, June 2020, July 2020, August 2020, September 2020, October 2020, November 2020, December
(this is the result i need)
My observation 1.when i am using with group by year(datetimecol),month(datetimecol) , not giving desired result ..might be because of datetime feild... 2.when i tired second query with GROUP BY EXTRACT(YEAR_MONTH FROM datetimecol) order by year(datetimecol)...its working absolutely fine.
in conclusion, for getting months by year wise use the following query.
select year( datetimecol) as Year,monthname(datetimecol) as Month from table GROUP BY EXTRACT(YEAR_MONTH FROM datetimecol) order by year(datetimecol) desc,month(datetimecol) asc;