2017-09-22 7 views
2

最初のログインから7日後に再度ログインしなかったユーザーの割合を示すレポートを生成しようとしています。現在、私は2017年1月1日〜2017年の間にログインしたユニークユーザーの数を示すSQLスクリプトを書くことができます。しかし、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

は、私はあなたが正確に何の出力が必要なのかわかりませんが、これはあなたが唯一の1つのログインを持つユニークユーザーを見つける、または第一および第二のログインの間で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日間にログインしたのかを知る必要がある場合、どのようにロジックを変更できますか?入力いただきありがとうございます。 –

関連する問題