PCage
PCage

Reputation: 21

MYSQL AVG by year

I have a table showing the daily sales:

CALENDAR   |         Sales
------ 
20170101   |           1
20170201   |           4
20170301   |           10
20170401   |           12

etc

How can I show the Daily sales per year?

The following query already produces monthly sales:

SELECT CALENDAR, AVG(sales)
FROM table
GROUP BY YEAR(CALENDAR), MONTH(CALENDAR)
HAVING COUNT(CALENDAR) = DAY(LAST_DAY(CALENDAR));

Upvotes: 1

Views: 1261

Answers (1)

PCage
PCage

Reputation: 21

After some searching on the internet, this is the solution:

SELECT EXTRACT(YEAR FROM CALENDAR) AS 'Year', AVG(Sales) FROM table GROUP BY YEAR(CALENDAR)

Upvotes: 1

Related Questions