获得avg最后4个日期范围的计数

时间:2016-03-09 09:41:33

标签: 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天的 AVG 。例如:今天是星期三,所以我将获得最后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

2 个答案:

答案 0 :(得分:1)

如果您想要今天,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)

答案 1 :(得分:0)

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