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

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

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

# 请你统计最大连续回答问题的天数大于等于3天的用户及其等级(若有多条符合条件的数据,按author_id升序排序),以上例子的输出结果如下:

# 先生成连续回答天数表

# 连续问题:

# 原理:就是等差数列问题,an=a0+nk

# 首先对天数dense_rank  相当于nk

# 任意天an 减去 nk (dense_rank  ) 都等于a0,就是a0组的连续天

# 求连续3天以上,就是count分组字段为初始日期(就是a0的字段),在having过滤

with t as (

select distinct author_id  

    ,answer_date dt

    ,dense_rank()over(partition by author_id order by answer_date) rk

from answer_tb

),t1 as(

select author_id

    ,date_sub(dt,interval rk day) as first_day

from t  )

,t2 as(

select author_id,count(first_day) days_cnt

from t1

group by  author_id

having count(first_day)>=3 )

select t2.author_id,author_level,days_cnt

from t2

join author_tb using(author_id)

全部评论

相关推荐

牛客62533758...:华为不卡双非,而是卡院校hhhh
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务