全部评论
select user_id, active_dt, if(count = 3,rank,1) active_days from ( select user_id,active_dt,rank, count(*)over(partition by user_id,sub_date) count from ( select user_id,active_dt,rank, (cast(active_dt as bigint) - rank) sub_date from (select user_id,active_dt, rank() over (partition by user_id order by active_dt) rank from table1) t1 ) t2 ) t3
select user_id,dt,row_number() over (partition by user_id ,rt order by active_dt ) as result from ( select user_id,dt,row_number() over (partition by user_id order by active_dt ) as rnt, cast(active_dt as int)-row_number() over (partition by user_id order by active_dt ) as rt from table1 ) a
第一感觉窗口函数可以做 就是麻烦些,要好几步。简单的要想一想
窗口函数增加新的排序一列 之后case when select a.user_id,a.active_dt,a.active_days from (select *, (case when active_day<=3 then active_day else 1 end)active_days from( select user_id, active_dt, row_number() over (partition by user_id order by active_dt )as active_day from table1) t)a ;
个人感觉这道题应该表达的是:连续三日活跃的,以1/2/3这样标出来。如果间断了活跃,就重新从1计数。
select user_id, active_at, row_number()over(partition by t1 order by active_at) from (select user_id, active_at, date_sub(activer_at,row_number()over(partition by user_id order by active_at)) as t1, count(1) from table1 group by user_id, active_at, date_sub(activer_at,row_number()over(partition by user_id order by active_at)) )
SELECT user_id, active_dt, row_number() OVER(PARTITION BY active_days ORDER BY active_dt) active_days FROM ( SELECT user_id, active_dt,TO_DATE(active_dt,'YYYYMMDD') - row_number() OVER(order by active_dt) active_days FROM table1 )
相关推荐