1. 背景
已知数据集为:
目的:
计算每个uid的连续活跃天数,并且每一段活跃期内的开始时间和结束时间
2. 步骤
第一步:处理数据集
处理数据集,使其满足每个uid每个日期只有一条数据。
第二步:以uid为主键,按照日期进行排序,计算row_number.
SELECT uid ,`征信查询日期` ,ROW_NUMBER() OVER(PARTITION BY uid ORDER BY `征信查询日期` ASC) AS `rn` ,first_value(`征信查询日期`)over(PARTITION BY uid ORDER BY `征信查询日期` ASC) `fir` ,first_value(`征信查询日期`)over(PARTITION BY uid ORDER BY `征信查询日期` desc) `las`FROM input
两个关键点:
- 序号rn可以看做一直活跃的情况下,活跃日期最大值和活跃日期最小值之间的天数差。那么,日期最大值与日期最小值之差如果不等于序号,就表明中间有不连续。
- 用’征信查询日期’ – rn 可以计算一列”关键列”,连续时间段内,它的关键列值是一样的
select *,DATE_SUB(`征信查询日期`,`rn`) as `关键列` from (SELECT uid ,`征信查询日期` ,ROW_NUMBER() OVER(PARTITION BY uid ORDER BY `征信查询日期` ASC) AS `rn` ,first_value(`征信查询日期`)over(PARTITION BY uid ORDER BY `征信查询日期` ASC) `fir` ,first_value(`征信查询日期`)over(PARTITION BY uid ORDER BY `征信查询日期` desc) `las`FROM input)
第三步:以uid和关键列作为主键。
select uid, `关键列`,count(*) as `连续活跃天数`, min(`征信查询日期`) as `活跃开始时间`, max(`征信查询日期`) as `活跃结束时间` from (select *, DATE_SUB(`征信查询日期`,`rn`) as `关键列` from (SELECT uid ,`征信查询日期` ,ROW_NUMBER() OVER(PARTITION BY uid ORDER BY `征信查询日期` ASC) AS `rn` ,first_value(`征信查询日期`)over(PARTITION BY uid ORDER BY `征信查询日期` ASC) `fir` ,first_value(`征信查询日期`)over(PARTITION BY uid ORDER BY `征信查询日期` desc) `las`FROM input ) )group by uid, `关键列`