SQL windows 函数 LEAD/LAG 但只考虑了某些值?

SQL windows functions LEAD/LAG but only taking certain values into account?

假设您有一个应用,想知道每次安装是否可以归因于新用户或回访用户。
因此,您想检查每个安装的 ID 是否有登录或稍后注册事件,以先到者为准。
你会如何从左 table 到右 table?
如果我可以说:

,那么使用 window 函数会很容易
 LAG(event,1) OVER (PARTITION BY id ORDER by event_timestamp)   

+但跳过 'install' 个事件,跳过它们

你会使用 lead(ignore nulls):

select t.*
from (select t.*,
             lead(case when event <> 'install' then event end ignore nulls) over (partition by id order by timestamp) as next_event
      from t
     ) t
where event = 'install';

ignore nulls 选项是 标准 SQL。但是,并非所有数据库都支持它。此类数据库中通常还有其他选项,但没有数据库标签,建议使用标准 SQL。