活动任务统计每一天用户连续完成任务量

运营有活动,每一天用户完成一个任务,给奖励;微信

须要统计每一天,连续完成任务的用户数
函数



需求以下:flex


数据解析以下:ui

抽一个case分析登陆完成状况:spa



借助 lag函数求解:.net


连续登录,其日期差相等;blog


代码以下:ci


with tmp_da_user as (get

select 数据分析

ds

,uid

,count(distinct task_name_day) as 任务量

from tmp_t_user_takse_detail

group by

ds

,uid

)



select

ds

,count(distinct uid) as 当天完成任务数

,count(case when DATEDIFF(ds,lag_1,'dd') = 1 then uid end ) as 连续2天完成

,count(case when DATEDIFF(ds,lag_2,'dd') = 2 then uid end ) as 连续3天完成

,count(case when DATEDIFF(ds,lag_3,'dd') = 3 then uid end ) as 连续4天完成

,count(case when DATEDIFF(ds,lag_4,'dd') = 4 then uid end ) as 连续5天完成

,count(case when DATEDIFF(ds,lag_5,'dd') = 5 then uid end ) as 连续6天完成

,count(case when DATEDIFF(ds,lag_6,'dd') = 6 then uid end ) as 连续7天完成

,count(case when DATEDIFF(ds,lag_7,'dd') = 7 then uid end ) as 连续8天完成

from(

select

uid

,ds

-- ,ds - row_number()over(partition by uid order by ds asc) as groupday

,lag(ds,1)over(partition by uid order by ds asc) as lag_1

,lag(ds,2)over(partition by uid order by ds asc) as lag_2

,lag(ds,3)over(partition by uid order by ds asc) as lag_3

,lag(ds,4)over(partition by uid order by ds asc) as lag_4

,lag(ds,5)over(partition by uid order by ds asc) as lag_5

,lag(ds,6)over(partition by uid order by ds asc) as lag_6

,lag(ds,7)over(partition by uid order by ds asc) as lag_7

from tmp_da_user

 where uid = '203111313364823804'

)t

group by

ds









本文分享自微信公众号 - SQL数据分析(dianwu_dw)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索