我有一个MYSQL数据库的查询,如下所示:
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...
COUNT(*) AS total FROM table order by total
然而,除了总数之外,我需要平均每月1到12个月。
编辑 - 我需要保留SUMMED数字(第1,2个等),但我还需要计算它们的平均值。
所以,如果month1-12的结果是 -
55,60,70,54,89,58,68,78,65,89,73,81 而TOTAL是840
我需要计算所有这些数字的平均值(70)
由于
答案 0 :(得分:0)
SELECT name,
AVG(IF(date_format (date, '%b, %Y')= 'Dec, 2011', 1,0)) AS `month1`,
AVG(IF(date_format (date, '%b, %Y')= 'Jan, 2012', 1,0)) AS `month2`,
AVG(IF(date_format (date, '%b, %Y')= 'Feb, 2012', 1,0)) AS `month3`,
-- etc...
COUNT(*) AS total FROM table order by total
答案 1 :(得分:0)
简单:
SELECT name,
AVG(IF(date_format (date, '%b, %Y')= 'Dec, 2011', 1,0)) AS `month1`,
AVG(IF(date_format (date, '%b, %Y')= 'Jan, 2012', 1,0)) AS `month2`,
AVG(IF(date_format (date, '%b, %Y')= 'Feb, 2012', 1,0)) AS `month3`,
etc...
COUNT(*) AS total FROM table order by total