每天创建的平均行数?

时间:2013-10-28 14:39:39

标签: mysql statistics

示例表结构:

+-------------------------+---------------+------+-----+-----------------------+-----------------------------+
| Field                   | Type          | Null | Key | Default               | Extra                       |
+-------------------------+---------------+------+-----+-----------------------+-----------------------------+
| id                      | char(25)      | NO   | PRI | NULL                  |                             |
| created_at              | timestamp     | NO   |     | CURRENT_TIMESTAMP     | on update CURRENT_TIMESTAMP |
+-------------------------+---------------+------+-----+-----------------------+-----------------------------+

如何找出每天平均添加的行数?

3 个答案:

答案 0 :(得分:28)

这将返回int个数字:

select count(*) / count(distinct date(created_at))
from your_table

要使用decimal号码:

select count(*) * 1.0 / count(distinct date(created_at))
from your_table

答案 1 :(得分:18)

SELECT AVG(rowsPerDay) AS avgPerDay
FROM ( SELECT 
         COUNT(*) AS rowsPerDay
       FROM tbl
       GROUP BY DATE(created_at)
     ) AS a
;

答案 2 :(得分:0)

如果几天的行数为0,这两种方法都不起作用,对吗?我想,如果您每天都需要,就可以计算出从第一天起的天数,并使用它

这在MariaDB中有效:

SELECT COUNT(*) / DATEDIFF ( CURRENT_DATE() , MIN(DATE(created_at)) ) AS "Avg. rows per day" 
FROM your_table;