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

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

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

select
b.author_id,
b.author_level,
max(cnt) as days_cnt
from
(
select 
a.author_id,
a.author_level,
count(date_result) as cnt
from(
select 
t.author_id,
t.author_level,
date_sub(answer_date,interval t.rk day) date_result
from(
select 
author_id,
author_level,
answer_date,
row_number() over(partition by author_id order by answer_date) as rk
#如果这里用dense_rank()就需要count(distinct answer_date)否则重复的用户无法去除,row_number()可以直接去重
from author_tb
left join answer_tb using(author_id)
) t    
) a
group by a.author_id,a.author_level,a.date_result    
)b
group by b.author_id,b.author_level
having max(cnt)>=3
order by author_id

#sql练习日常#
全部评论

相关推荐

10-09 19:35
门头沟学院 Java
洛必不可达:java的竞争激烈程度是其他任何岗位的10到20倍
点赞 评论 收藏
分享
程序员猪皮:看不到八股什么意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务