Display month of sale and number of sales done in that month sorted in descending order of sales.
I have used the extract function to extract month from date but it gives month number not the full name of the month
select extract(month from sldate) as month, count(sid) as number_sale from sale
group by sldate
order by sid desc
this is the table
Sale Table
SALEID SID SLDATE
1001 1 01-JAN-14
1002 5 02-JAN-14
1003 4 01-FEB-14
1004 1 01-MAR-14
1005 2 01-FEB-14
1006 1 01-JUN-15
Expected Result
MONTH NUMBER_SALE
February 2
January 2
March 1
June 1
This answers the original question tagged with MySql.
Instead of extracting just the month, you need to combine it with the year, because you don't want to mix sales of the same month in different years, so use year_month
and then use monthname()
to get the month's name:
select
monthname(concat(extract(year_month from sldate), '01')) as month,
count(sid) as number_sale
from sale
group by month
order by number_sale desc
See the demo.
Results:
| month | number_sale |
| -------- | ----------- |
| February | 2 |
| January | 2 |
| March | 1 |
| June | 1 |