代码
with t1 as(
select distinct device_id,date --去重防止单日多次答题的情况
from question_practice_detail
)
select
avg(if(datediff(date2,date1)=1,1,0)) as avg_ret
from (
select
distinct device_id
,date as date1
,lead(date) over(partition by device_id order by date) as date2
from t1
) as b
知识点
- distinct后面的所有列都将进行去重操作
- if函数的用法
- datediff函数的用法(参数需为日期类型)
- lead函数:本身有三个参数(所取的字段,向下偏移量【默认为1】,默认值)
该函数经常用于留存率分析