题解 | 任意两个连续自然月练题次数大于1的用户

任意两个连续自然月练题次数大于1的用户

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

select distinct device_id from
(select device_id,year1,month1,lag(month1)over(partition by device_id order by year1,month1) month2,lag(year1)over(partition by device_id order by year1,month1) year2 from
(select device_id,year(event_date) year1,month(event_date) month1,count(question_id) cnt from question_practice_detail
group by device_id,year(event_date),month(event_date)
having cnt>1) as newtable) as new
where (month1-month2=1 and year1=year2) or (month1-month2=-11 and year1-year2=1)
order by device_id desc

有两种情况会是连续的月份:1、本年内的两个月 2、12月和次年1月。

(month1-month2=1 and year1=year2) or (month1-month2=-11 and year1-year2=1)

month1为用month()取出的当前日期月份,month2是用lag()over()窗口函数取出的上一个做题数大于1的月份。

year同理

全部评论

相关推荐

06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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