Skip to content
Advertisement

MySQL = Grouping Sales Count by Months, then Displaying months as Names (Jan, Feb…DEC) not Numerical Value (1,2,3-12)

This seems like a reasonably simple problem but I cannot figure out how to change a numerical value of a month into the months actual name after the sales count is grouped by the month.

Basically I want to list the total amount of sales for each month. Currently this is what I have:

This works great for what I want to do. And gives this results:

However I want the list to have the months as January, February…etc.

I’ve tried multiple ways using MONTHNAME(), MONTHNAME(STR_TO_DATE) and many others, but grouping the months seems to cause the issue.

I feel like I’m missing a very simple solution but am unable to find any online. For reference the dates in the Sale table are default (2017-01-01 or YYYY-MM-DD). And the sale table has three attributes: SaleID, StoreID and SaleDate.

Here’s the error message I recieve from the methods I’ve tried:

Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘UsedCarDealer.Sale.SaleDate’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Thanks!

Advertisement

Answer

I would suggest:

This will also return the values in chronological order. Note: When using months, it usually makes sense to include the year as a filter or in the GROUP BY. Perhaps a better version of the query is:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement