题解 | 任意两个连续自然月练题次数大于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同理

全部评论

相关推荐

Hyh_111:像这种hr就不用管了,基本没啥实力,换一个吧
点赞 评论 收藏
分享
09-06 12:49
东北大学 Java
offeroffer...:我也是,前两面还挺紧张认真的,全程大脑飞速运转后面就越来越不想面了,不想说话不想思考
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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