我想找到过去26周内每周结束时的用户总数。
例如,
等等
我可以找到每周新用户的数量,但是如何让它向我展示总数(即本周新增+本周之前的所有用户)等等,而不必创建26个单独选择?可能吗?
我发现Grouping total active users for each of the previous 8 weeks但它适用于SQL Server,我认为它并不是我所需要的。
我每周都会使用它来获取新内容:
select count(id) as total
from users
where join_date>='$sixmonths'
group by WEEK(join_date)
order by WEEK(join_date) desc
limit 26
答案 0 :(得分:1)
查询:
select count(u.id) as total,
(SELECT COUNT(u.id)
FROM users u2
WHERE WEEK(u2.join_date) <=
WEEK(u.join_date)
AND u2.id = u.id) AS Total_count
from users u
where u.join_date>='$sixmonths'
group by WEEK(u.join_date)
order by WEEK(u.join_date) desc
limit 26
让Query在多年之间工作:
select count(u.id) as total,
(SELECT COUNT(u.id)
FROM users u2
WHERE YEARWEEK(u2.join_date) <=
YEARWEEK(u.join_date)
AND u2.id = u.id) AS Total_count
from users u
where u.join_date>='$sixmonths'
group by YEARWEEK(u.join_date)
order by YEARWEEK(u.join_date) desc
limit 26