![](http://static.javashuo.com/static/loading.gif)
運營有活動,每一天用戶完成一個任務,給獎勵;微信
須要統計每一天,連續完成任務的用戶數
函數
需求以下: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源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。