题解 | #某乎问答最大连续回答问题天数大于等于3天的用户及其对应等级#
某乎问答最大连续回答问题天数大于等于3天的用户及其对应等级
http://www.nowcoder.com/practice/e080f8a685bc4af3b47749ca3310f1fd
- 经典连续登录问题解决
- 对于俩表连接,为answer表拼接level标签列
- 进行窗口函数的计算,计算出每位用户登录日期的排名
- 根据用户id,登录日期与排名的差值分组计数(只要是连续登录,登录日期与排名的差值为同一天)
select b.author_id,b.author_level,count(*) days_cnt
FROM(
select a.author_id,a.author_level,a.answer_date,row_number() over(partition by a.author_id order by a.answer_date) rk
from(
select distinct answer_date,author_id,author_level
from author_tb
join answer_tb using(author_id)
) a
)b
group by b.author_id,(b.answer_date-rk)
having days_cnt>=3