LeetCode(数据库)- 每日新用户统计
程序员文章站
2022-07-14 15:31:55
...
题目链接:点击打开链接
题目大意:略。
解题思路:这题有点坑,首先题目描述不清楚是在倒退90天到今天为止,还是从今天开始算未来的90天;接着既然是倒退90天内,按照计算应该是到 '2019-04-02' 为止,包括今天的话,可是居然是到 '2019-04-01',这样算的话包括今天就是 91 天了。
AC 代码
-- 解决方案(1)
WITH t1 AS(SELECT *, ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY activity_date) rw
FROM Traffic
WHERE activity = 'login')
SELECT activity_date login_date, COUNT(DISTINCT user_id) user_count
FROM t1
WHERE rw = 1 AND activity_date BETWEEN '2019-04-01' AND '2019-06-30'
GROUP BY activity_date
-- 解决方案(2)
select login_date,count(user_id) user_count
from (select user_id, min(activity_date) login_date from Traffic
where activity='login'
group by user_id) t
where datediff('2019-06-30',login_date)<=90
group by login_date;