如何获取两个特定事件之间的事件顺序?
How to get the event sequence between two specific events?
我正在尝试从 Firebase 查询事件数据。目标是获取以事件 X 开始,以事件 Y 结束的事件序列。事件按日期排序。
示例数据:
user_id
event_name
date
1
X
1
b
1
c
1
Y
2
a
2
Y
3
X
3
a
3
b
4
X
4
b
4
Y
理想输出:
user_id
event_name
date
1
X
1
b
1
c
1
Y
4
X
4
b
4
Y
考虑以下方法
select * except(group_id) from (
select *,
countif(event_name = 'X') over(partition by user_id order by date rows between unbounded preceding and current row) -
countif(event_name = 'Y') over(partition by user_id order by date rows between unbounded preceding and 1 preceding) group_id,
from your_table
)
qualify 2 = countif(event_name in ('X', 'Y')) over(partition by user_id, group_id)
如果应用于您问题中的示例数据 - 输出为
我正在尝试从 Firebase 查询事件数据。目标是获取以事件 X 开始,以事件 Y 结束的事件序列。事件按日期排序。
示例数据:
user_id | event_name | date |
---|---|---|
1 | X | |
1 | b | |
1 | c | |
1 | Y | |
2 | a | |
2 | Y | |
3 | X | |
3 | a | |
3 | b | |
4 | X | |
4 | b | |
4 | Y |
理想输出:
user_id | event_name | date |
---|---|---|
1 | X | |
1 | b | |
1 | c | |
1 | Y | |
4 | X | |
4 | b | |
4 | Y |
考虑以下方法
select * except(group_id) from (
select *,
countif(event_name = 'X') over(partition by user_id order by date rows between unbounded preceding and current row) -
countif(event_name = 'Y') over(partition by user_id order by date rows between unbounded preceding and 1 preceding) group_id,
from your_table
)
qualify 2 = countif(event_name in ('X', 'Y')) over(partition by user_id, group_id)
如果应用于您问题中的示例数据 - 输出为