php - 获取日期范围内最后 4 次计数的平均值

标签 php mysql sql

我有查询统计某个日期范围内每一天注册的用户数。

select count(*) as c, date(from_unixtime(user.firstaccess)) as date
    from user
    where firstaccess between ? and ?
    group by date(from_unixtime(user.firstaccess))

这很好用。 现在,我正在生成报告,我想知道一周最后 4 天的平均值。例如:今天是星期三,所以我会得到最后 4 个星期三并得到 AVG。 同样,我有这个查询,正在运行,但我只能考虑每天单独查询的逻辑。而不是日期范围。 这就是我拥有的

select
(
(select count(*) from user where firstaccess between 1456617600-(3600*24*7) and 1456703999-(3600*24*7)) + 
(select count(*) from user where firstaccess between 1456617600-(3600*24*14) and 1456703999-(3600*24*14)) +
(select count(*) from user where firstaccess between 1456617600-(3600*24*14) and 1456703999-(3600*24*14)) +
(select count(*) from user where firstaccess between 1456617600-(3600*24*21) and 1456703999-(3600*24*21))
)
/4
as c
from user
limit 0,1

我正在使用 Mysql 和 PHP

最佳答案

如果你想要今天、7天、14天前和21天前首次注册的用户的平均数,你可以使用:

select count(*)/4 as avg_new_users
from
user
where 
date(from_unixtime(user.firstaccess))=CURDATE() or date(from_unixtime(user.firstaccess))=DATE_SUB(CURDATE(),INTERVAL 7 DAY) or date(from_unixtime(user.firstaccess))=DATE_SUB(CURDATE(),INTERVAL 14 DAY) or date(from_unixtime(user.firstaccess))=DATE_SUB(CURDATE(),INTERVAL 21 DAY)

关于php - 获取日期范围内最后 4 次计数的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35887917/

相关文章:

javascript - 具有单独操作值的 Ajax 表单

php - WordPress 搜索功能仅搜索帖子

php - 如何使用 Linkedin API 获取 Linkedin 推荐?

php - MySQL 或 PHP : Find optimum combination of rows based on score

mysql - 某个日期范围内每个用户每天的登录次数

sql - 2 选择或 1 加入查询?

php - 在 PHP 中清理查询字符串

mysql - 在风 sails 升降机上停止柱/表变化的方法

mysql - NOT IN 子句和 NULL 值

mysql - 这个 phpBB 的 SQL 有什么问题?