题解 |最大连续回答问题天数大于等于3天的用户

某乎问答最大连续回答问题天数大于等于3天的用户及其对应等级

https://www.nowcoder.com/practice/e080f8a685bc4af3b47749ca3310f1fd

# 二刷:
# tb1 : 连接完表 、 构造连续编号
with tb1 as(
   select distinct author_id,author_level,answer_date ,
      ## 按照登录进行排序 
   dense_rank()over(partition by author_id,author_level order by answer_date asc) as rk,
      ## 构造日期answer_date和编号rk的差值
   date_sub(answer_date,interval dense_rank()over(partition by author_id,author_level order by answer_date asc) day) as diff
   from answer_tb left join author_tb using(author_id)
   ),

# tb2 : 按照author_id和diff进行分组
tb2 as(
   select author_id,author_level,count(*) as ct 
   from tb1
   group by author_id,author_level,diff
   )
# tb3 : 选取最大登录天数大于等于3的用户
select author_id,author_level,max(ct) as max_ct
from tb2
group by  author_id,author_level
having max_ct >= 3



全部评论

相关推荐

牛客717484937号:双飞硕没实习挺要命的
点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务