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

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

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

写的可能有点乱,但是核心思想很简单,就是构造一个递增序列,让日期和这个序列做差,因为日期是递增的,序列也是递增的,所以只要日期连续,那么差值是相等的。

另外需要注意的是Group by的对象,t1表中用group by 去重 , 最后一层用group by 统计每个人差值相等的天数

t1: 去掉一天多次答题的用户,只保留一次

t2:构造带有递增序列的表

t3: 通过日期和递增序列作差得到标签,标签相同则连续

select

    author_id,

    (select author_level from author_tb where author_id=t3.author_id) author_level,

    count(*) days_cnt

from

    (select author_id,date_format(answer_date,'%Y%m%d')-rk label

    from (select author_id,answer_date,row_number() over(partition by author_id) rk

        from (select answer_date,author_id

              from answer_tb

              group by answer_date,author_id) t1) t2) t3

group by label,author_id

having count(*)>=3

order by author_id asc

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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