Solved: Get average by month on a select.

Post Reply
mister_v
Posts: 195
Joined: Thu Mar 04, 2010 9:19 pm

Solved: Get average by month on a select.

Post by mister_v »

Hi,

I have a table with date for every day.
But I want the average by month.

Is there a simple query that can do that,
Or do I have to calculate it on the application layer?
chris
Site Admin
Posts: 202
Joined: Mon Jul 21, 2008 9:52 am

Re: Get average by month on a select.

Post by chris »

You can do it in one query:

Code: Select all

SELECT id,year(date),month(date),avg(price) FROM table GROUP BY id,year(date),month(date)
I haven't compared the speed,
but it is probably faster then to do it in the application.
Post Reply