0

我有一个 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

但是,我需要平均month1到month12而不是总数。

编辑 - 我需要保留求和的数字(month1,2 等),但我还需要计算它们的平均值。

因此,如果第 1-12 个月的结果是 -

55, 60, 70, 54, 89, 58, 68, 78, 65, 89, 73, 81 总数为 840

我需要计算所有这些数字的平均值 (70)

谢谢

4

2 回答 2

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
于 2013-01-16T14:16:01.573 回答
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
于 2013-01-16T14:16:02.343 回答