2017-09-22 58 views
2

我試圖生成報告,顯示初次登錄後7天未再登錄的用戶的百分比。目前,我可以編寫一個SQL腳本,顯示2017年1月1日至2017年1月1日期間登錄的唯一身份用戶數。但是,如何查找7天后未再登錄的用戶數?由於生成報告,顯示初次登錄後7天未再次登錄的用戶的百分比

select 
    count (distinct a.user_id) as unique_user_ids_logins_in_month, 
    to_char(first_hit_at::date,'dd-mm-yyyy') as date 
from 
    stg_marketing.ga_sessions a 
where 
    a.first_hit_at >('2017-01-01 00:00:00.000') 
    and a.first_hit_at <('2017-02-01 00:00:00.000') 
    and user_login_state = 'true' 
    and last_hit_at::date > first_hit_at::date 
group by 2 
order by 2 asc 


Unique_user_logins Date 
    97     01-01-2017 
    96     02-01-2017 
    62     03-01-2017 
    61     04-01-2017 
    69     05-01-2017 
    65     06-01-2017 
    75     07-01-2017 
    82     08-01-2017 
+0

你需要什麼作爲輸出?在首次登錄後7天內沒有登錄的用戶數量?請將預期的輸出添加到問題中。 –

+0

@VamsiPrabhala是,未來7天內未再次登錄的用戶數量。謝謝 –

回答

0

我不知道是什麼做的輸出你到底需要,但是這是你如何能找到的唯一用戶,誰都有隻需登錄一次,或有超過第一和第二登錄之間的7天:

with t(user_id, login_dt) as(
    select 1, '2017-01-01 02:00:00'::timestamp union all 
    select 1, '2017-01-05 02:00:00'::timestamp union all 
    select 2, '2017-01-01 02:00:00'::timestamp union all 
    select 2, '2017-01-09 02:00:00'::timestamp union all 
    select 3, '2017-01-01 02:00:00'::timestamp union all 
    select 3, '2017-01-05 02:00:00'::timestamp union all 
    select 4, '2017-01-01 02:00:00'::timestamp union all 
    select 4, '2017-01-10 02:00:00'::timestamp union all 
    select 4, '2017-01-11 02:00:00'::timestamp union all 
    select 22, '2017-01-10 02:00:00'::timestamp union all 
    select 22, '2017-01-10 02:00:04'::timestamp 

) 


select user_id from (
    select user_id, login_dt::date, row_number() over(partition by user_id order by login_dt) as rn 
    from t 
    where 
    login_dt >= '2017-01-01' and login_dt < '2017-02-01' 
) t 
where 
rn < 3 
group by user_id 
having 
count(login_dt) = 1 
or 
min(login_dt) + 7 < max(login_dt) 

演示:http://rextester.com/RKEHTZ93773

+0

感謝您的答覆隊友。如果我需要查看誰在接下來的7天內登錄,我該如何更改邏輯以防萬一?感謝您的意見。 –

相關問題