我的表有一个user_id,first_hit_at :: date和last_hit_at :: date,如下所示。
user_id . first_hit_at . last_hit_at
1 2017-01-01 2017-01-01
2 2017-01-01 2017-01-01
3 2017-01-01 2017-01-01
4 2017-01-01 2017-01-01
5 2017-01-01 . 2017-01-01
6 2017-01-01 2017-01-01
7 2017-01-01 . 2017-01-01
8 2017-01-01 . 2017-01-01
9 2017-01-01 . 2017-01-01
期望的输出:
count(distinct_id) date
3 2017-01-01
2 2017-01-02
1 2017-01-03
2 2017-01-04
我想计算今天登录的用户,他们还会在接下来的7天内至少登录一次 我写的SQL查询:
select
a.user_id,
first_hit_at::date,
row_number() over(partition by a.user_id order by first_hit_at) as rn
from stg_marketing.ga_sessions a
where first_hit_at::date between '2017-01-01' and '2017-01-31'
and user_login_state = 'true'
--and first_hit_at::date > '7'
order by 2,3 asc;
答案 0 :(得分:0)
这是对问题的原始版本的回答。
我会考虑聚合。像这样:
select user_id
from stg_marketing.ga_sessions s
where s.first_hit_at >= current_date - interval '7 day'
group by s.user_id
having max(first_hit_at) >= current_date and
min(first_hit_at) < current_date();