SQL59 截至当月的练题情况

img


with t1 as
(select device_id,date_format(event_date,'%Y-%m') ym
from question_practice_detail
group by device_id,ym
having count(question_id)>1)
t2 as
(select device_id,ym,
lag(ym,1)over(partition by device_id order by ym) ym2
from t1
where period_diff(ym, ym2) = 1
select distinct device_id
from t2

不知道哪里错了
with t as (select device_id, date_format(event_date, '%Y-%m') as mon, 
count(*) as cnt from question_practice_detail
group by device_id, mon having cnt > 1)
select distinct a.device_id from t a join t b 
on a.device_id = b.device_id 
and timestampdiff(month, concat(a.mon, '-01'), concat(b.mon, '-01')) = 1
order by device_id desc;