问题描述
|
我需要获得以下比率(每日注册计数)/(最近30天每天的滚动注册计数)
每日数字直接
SELECT a.DailySignup
FROM
(
SELECT COUNT(1) AS DailySignup,date
FROM users
WHERE date BETWEEN datestart and dateend
GROUP BY date
) a
但是如何计算最近30天的每一天?我知道我需要再次将它加入同一张桌子,但我似乎无法将其围绕如何使用。
更新
抱歉,我不太清楚。
SIGNUP_COUNT DATE Rolling_Signup_Count
10 2011-01-01 Sign ups from 2010-12-01 to 2011-01-01
12 2011-01-02 Sign ups from 2010-12-02 to 2011-01-02
4 2011-01-03 Sign ups from 2010-12-03 to 2011-01-03
希望此表有助于说明我的意思是滚动注册计数
解决方法
您可以尝试子查询:
SELECT DISTINCT t.date,(SELECT COUNT(*) FROM users u where u.date BETWEEN DATE_ADD(t.date,INTERVAL -30 day) AND t.date) as c
FROM users t
对于给定的日期t.date,子查询将计算以t.date结尾的30天期间的计数。
编辑:要计算比率(每天登录)/(30天之内登录),您可以尝试
SELECT t.date,count(*)/(SELECT COUNT(*) FROM users u
WHERE u.date BETWEEN DATE_ADD(t.date,INTERVAL -30 day) AND t.date) as c
FROM users t GROUP BY t.date
, 您可以按MONTH(date)
分组以获得每个月的收入,请尝试如下操作:
SELECT a.DailySignup,month AS MONTH(date)
FROM users
INNER JOIN (
SELECT COUNT(*) AS DailySignup,MONTH(date) AS month
FROM users
GROUP BY MONTH(date)
) a ON a.month = month
GROUP BY month