平均值 - Mysql

时间:2017-01-12 09:50:51

标签: mysql

我有一个SQL查询,如下所示:

SELECT COUNT(*)
FROM event_db.event_log el, event_db.event_type et
WHERE et.event_type_id = el.event_type_id AND et.name in ('kpi_stats_dumped') AND el.timestamp > str_to_date('2016,12,12','%Y,%m,%d') AND el.timestamp < str_to_date('2017,12,12','%Y,%m,%d')
GROUP BY et.name, date_format(el.timestamp, '%d-%m-%Y')
ORDER BY et.name, el.timestamp

将返回结果如下:

1440
1441
1546
1234
1235
33
224

我需要找到列出的值的平均值。(1021.85714286)

有人可以帮助我。

2 个答案:

答案 0 :(得分:1)

select avg(cnt) as average_count
from 
(
    SELECT COUNT(*) as cnt
    FROM event_db.event_log el
    JOIN event_db.event_type et ON et.event_type_id = el.event_type_id
    WHERE et.name in ('kpi_stats_dumped') 
    AND el.timestamp > str_to_date('2016,12,12','%Y,%m,%d') 
    AND el.timestamp < str_to_date('2017,12,12','%Y,%m,%d')
    GROUP BY et.name, date_format(el.timestamp, '%d-%m-%Y')
) tmp

答案 1 :(得分:0)

我得到了答案,现在查询如下:

SELECT avg(cnt) from 
(SELECT count(*) as cnt
FROM  event_db.event_log el ,
      event_db.event_type et
WHERE et.event_type_id = el.event_type_id
      AND et.name IN ( 'kpi_stats_dumped' )
      AND el.timestamp > str_to_date('2016,12,12', '%Y,%m,%d')
      AND el.timestamp < str_to_date('2017,12,12', '%Y,%m,%d')
GROUP BY et.name , date_format(el.timestamp, '%d-%m-%Y')
ORDER BY et.name, el.timestamp) tmp