我有以下MySQL查询,该查询显示了按周分组的新用户数。
这是查询:
SELECT floor(datediff(u.created_at, curdate()) / 7) AS weeks_ago,
count(DISTINCT u.id) AS "New Users in Week"
FROM users u
GROUP BY weeks_ago
ORDER BY weeks_ago DESC;
这将输出:
weeks_ago | New Users in Week
0 | 100
1 | 120
2 | 50
如何添加显示日期范围的列,例如,如果今天运行(2019/07/16)
weeks_ago | date range | New Users in Week
0 | 2019/07/14 - 2019/07/20 | 100
1 | 2019/07/7 - 2019/07/13 | 120
2 | 2019/06/30 - 2019/07/6 | 50
.....
答案 0 :(得分:1)
两列的最小日期和最大日期如何?
SELECT floor(datediff(u.created_at, curdate()) / 7) AS weeks_ago,
date(min(u.created_at)), date(max(u.created_at))
count(DISTINCT u.id) AS "New Users in Week"
FROM users u
GROUP BY weeks_ago
ORDER BY weeks_ago DESC;