如何 Select 基于前一个匹配行的值的行?
How to Select a Row Based on Value From previous matched row?
假设如下 table:
Action, Action_Timestamp
act_b 1
act_a 2
act_x 3
act_b 4
act_c 6
act_b 7
act_c 8
我想知道是否有这样的动作序列(它们之间有动作):
`act_a -> act_b -> act_c` where `act_a_timestamp < act_b_timestamp < act_b_timestamp` (aka first occurrence of each event)
所以结果应该是这样的:
Action, Action_Timestamp
act_a 2
act_b 4
act_c 6
你能帮我写SQL查询以获得上面显示的结果吗?
P.s。我的 RDBMS 中没有限制
你没有说明你的 DBMS,所以这是标准的 SQL:
select action, action_timestamp
from (
select action, action_timestamp,
row_number() over (partition by action order by action_timestamp) as rn
from the_table
) t
where rn = 1;
假设如下 table:
Action, Action_Timestamp
act_b 1
act_a 2
act_x 3
act_b 4
act_c 6
act_b 7
act_c 8
我想知道是否有这样的动作序列(它们之间有动作):
`act_a -> act_b -> act_c` where `act_a_timestamp < act_b_timestamp < act_b_timestamp` (aka first occurrence of each event)
所以结果应该是这样的:
Action, Action_Timestamp
act_a 2
act_b 4
act_c 6
你能帮我写SQL查询以获得上面显示的结果吗?
P.s。我的 RDBMS 中没有限制
你没有说明你的 DBMS,所以这是标准的 SQL:
select action, action_timestamp
from (
select action, action_timestamp,
row_number() over (partition by action order by action_timestamp) as rn
from the_table
) t
where rn = 1;