WITH a AS ( SELECT distinct device_id, date FROM question_practice_detail ), c as ( select *,RANK() OVER(PARTITION BY device_id ORDER BY date) AS rk, DATE_ADD(date, INTERVAL 1 DAY) AS d_plus_1 from a ), b AS ( SELECT *, LEAD(date, 1) OVER(PARTITION BY device_id ORDER BY ...