user3066913
user3066913

Reputation:

Get the Average of a Column made by Group By Date()

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

This query returns something like :

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

How may i edit the query to return the average among this values ?

Thanks.

Upvotes: 0

Views: 62

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269503

Here is an alternative way that doesn't require a subquery:

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

This manually calculates the average by taking the overall total and dividing by the number of times.

Upvotes: 2

juergen d
juergen d

Reputation: 204746

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

Upvotes: 3

Related Questions