获取按日期分组的列的平均值()

时间:2013-12-28 13:50:02

标签: mysql

SELECT COUNT(Time)
FROM  tablename
WHERE DATE(Time) BETWEEN '2013-07-14' and '2013-12-23' GROUP BY DATE(Time)

此查询返回类似于:

的内容
COUNT(Time)
'302'
'1322'
'145'
'670'
'1091'
'589'
'1101'
'324'

如何编辑查询以返回此值中的平均值?

感谢。

2 个答案:

答案 0 :(得分:3)

select avg(cnt_time)
from
(
   SELECT COUNT(Time) as cnt_time
   FROM  tablename
   WHERE DATE(Time) BETWEEN '2013-07-14' and '2013-12-23' 
   GROUP BY DATE(Time)
) alias_name

答案 1 :(得分:2)

以下是一种不需要子查询的替代方法:

SELECT COUNT(Time) / count(distinct DATE(Time))
FROM  tablename
WHERE DATE(Time) BETWEEN '2013-07-14' and '2013-12-23';

通过取总数并除以次数来手动计算平均值。