来自详细说明 THING 变化的行的 THING 每日活跃计数
Daily active counts of THING from rows detailing change of THING
假设我有 table 人在第 N 天使用过我的服务,并且 table 描述了用户更改的主题。没有 table 可以告诉我他们当前使用的是什么主题。我想做的是每天都看到它。
让我们假设变化 table 看起来像这样。
| user_ID | date | theme |
|---------|----------|-------|
| user1 | 1.1.2021 | Dark |
| user1 | 4.1.2021 | Light |
| user2 | 2.1.2021 | Dark |
| user2 | 6.1.2021 | Light |
activity table 只报告了 user_ID 和访问服务的日期。
| user_ID | date |
|---------|----------|
| user1 | 1.1.2021 |
| user1 | 2.1.2021 |
| user1 | 3.1.2021 |
| user1 | 4.1.2021 |
| user1 | 5.1.2021 |
| user1 | 6.1.2021 |
| user2 | 2.1.2021 |
| user2 | 3.1.2021 |
| user2 | 4.1.2021 |
| user2 | 5.1.2021 |
| user2 | 6.1.2021 |
现在我想做的是将第一个 table 加入第二个,这样他们在活动日期使用的主题就会列在那里。
| user_ID | date | theme |
|---------|----------|-------|
| user1 | 1.1.2021 | Dark |
| user1 | 2.1.2021 | Dark |
| user1 | 3.1.2021 | Dark |
| user1 | 4.1.2021 | Light |
| user1 | 5.1.2021 | Light |
| user1 | 6.1.2021 | Light |
| user2 | 2.1.2021 | Dark |
| user2 | 3.1.2021 | Dark |
| user2 | 4.1.2021 | Dark |
| user2 | 5.1.2021 | Dark |
| user2 | 6.1.2021 | Light |
如何实现?假设可以有无限数量的主题。
一种方法是相关子查询,但我不确定Presto是否支持这个:
select a.*,
(select c.theme
from changes c
where c.user_id = a.user_id and
c.date <= a.date
order by c.date desc
limit 1
) as theme
from activity a;
也许更有效的方法是使用 left join
但要计算每次更改的“结束”日期:
select a.*, c.theme
from activity a left join
(select c.*,
lead(date) over (partition by user_id order by date) as next_date
from changes c
) c
on a.user_id = c.user_id and
a.date >= c.date and
(a.date < c.next_date or c.next_date is null);
假设我有 table 人在第 N 天使用过我的服务,并且 table 描述了用户更改的主题。没有 table 可以告诉我他们当前使用的是什么主题。我想做的是每天都看到它。
让我们假设变化 table 看起来像这样。
| user_ID | date | theme |
|---------|----------|-------|
| user1 | 1.1.2021 | Dark |
| user1 | 4.1.2021 | Light |
| user2 | 2.1.2021 | Dark |
| user2 | 6.1.2021 | Light |
activity table 只报告了 user_ID 和访问服务的日期。
| user_ID | date |
|---------|----------|
| user1 | 1.1.2021 |
| user1 | 2.1.2021 |
| user1 | 3.1.2021 |
| user1 | 4.1.2021 |
| user1 | 5.1.2021 |
| user1 | 6.1.2021 |
| user2 | 2.1.2021 |
| user2 | 3.1.2021 |
| user2 | 4.1.2021 |
| user2 | 5.1.2021 |
| user2 | 6.1.2021 |
现在我想做的是将第一个 table 加入第二个,这样他们在活动日期使用的主题就会列在那里。
| user_ID | date | theme |
|---------|----------|-------|
| user1 | 1.1.2021 | Dark |
| user1 | 2.1.2021 | Dark |
| user1 | 3.1.2021 | Dark |
| user1 | 4.1.2021 | Light |
| user1 | 5.1.2021 | Light |
| user1 | 6.1.2021 | Light |
| user2 | 2.1.2021 | Dark |
| user2 | 3.1.2021 | Dark |
| user2 | 4.1.2021 | Dark |
| user2 | 5.1.2021 | Dark |
| user2 | 6.1.2021 | Light |
如何实现?假设可以有无限数量的主题。
一种方法是相关子查询,但我不确定Presto是否支持这个:
select a.*,
(select c.theme
from changes c
where c.user_id = a.user_id and
c.date <= a.date
order by c.date desc
limit 1
) as theme
from activity a;
也许更有效的方法是使用 left join
但要计算每次更改的“结束”日期:
select a.*, c.theme
from activity a left join
(select c.*,
lead(date) over (partition by user_id order by date) as next_date
from changes c
) c
on a.user_id = c.user_id and
a.date >= c.date and
(a.date < c.next_date or c.next_date is null);