(This is a rewrite of my previous question which may not have been clear enough)
I have a query for a MYSQL database which is as follows:
SELECT name,
SUM(IF(date_format (date, '%b, %Y')= 'Dec, 2011', 1,0)) AS `month1`,
SUM(IF(date_format (date, '%b, %Y')= 'Jan, 2012', 1,0)) AS `month2`,
SUM(IF(date_format (date, '%b, %Y')= 'Feb, 2012', 1,0)) AS `month3`,
etc...
Which gets me a series of results like – month1=55, month2=70, month3=89 etc
In the query is a line –
COUNT(*) AS total FROM table order by total
Which effectively gives me a total of month1+month2+month3+ etc
However I also need to get an average of those same monthly totals
So I need a MySQL function that would effectively be something like
AVG (month1, month2, month3 etc)
which would give the average of 55,70,89
Can anyone help?
Thanks very much
AS REQUESTED, COMPLETE QUERY IS –
SELECT name,
SUM(IF(date_format (date, '%b, %Y')= 'Nov, 2011', 1,0))/list*1000 AS `month1`,
SUM(IF(date_format (date, '%b, %Y')= 'Dec, 2011', 1,0))/list*1000 AS `month2`,
SUM(IF(date_format (date, '%b, %Y')= 'Jan, 2012', 1,0))/list*1000 AS `month3`,
SUM(IF(date_format (date, '%b, %Y')= 'Feb, 2012', 1,0))/list*1000 AS `month4`,
SUM(IF(date_format (date, '%b, %Y')= 'Mar, 2012', 1,0))/list*1000 AS `month5`,
SUM(IF(date_format (date, '%b, %Y')= 'Apr, 2012', 1,0))/list*1000 AS `month6`,
SUM(IF(date_format (date, '%b, %Y')= 'May, 2012', 1,0))/list*1000 AS `month7`,
SUM(IF(date_format (date, '%b, %Y')= 'Jun, 2012', 1,0))/list*1000 AS `month8`,
SUM(IF(date_format (date, '%b, %Y')= 'Jul, 2012', 1,0))/list*1000 AS `month9`,
SUM(IF(date_format (date, '%b, %Y')= 'Aug, 2012', 1,0))/list*1000 AS `month10`,
SUM(IF(date_format (date, '%b, %Y')= 'Sep, 2012', 1,0))/list*1000 AS `month11`,
SUM(IF(date_format (date, '%b, %Y')= 'Oct, 2012', 1,0))/list*1000 AS `month12`,
COUNT(*) AS total
FROM table
group by name
order by total
In your case you can use a subquery –
But I’d suggest you to use something like this –
…you only should add year support.