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

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个星期三的平均数。
同样,我有一个查询,可以工作,但是我可以思考每天单独查询的逻辑。没有日期范围。
这就是我所拥有的
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)

10-04 14:56
查看更多