select count(if(datediff(next_date,date) = 1,1,null))/count(*) avg_ret from( select device_id,date,lead(date,1) over(partition by device_id order by date) next_date from question_practice_detail group by device_id,date ) a 个人感觉我这样写更清晰一点