SQL 用条件填充前导行

SQL populating leading rows with conditions

我有一个 table 如下所示

如果第 condition 列被填充,则对于该行之后的所有行,如果 other condition 列包含 x,则用值 flag 填充第 new 列该行的 episode startepisode end 与较早的最近行匹配,其中 condition 列包含 yes

最终输出:

index   name    condition   episode_start   Episode_end other_condition  value     new
1         a                           11       12           
2         a                            1        2           
3         a          yes               1        2           
4         a                            1        2           
5         a                            1        2              x          f       flag
6         a                            3        4              b          cc    

#update1 下面建议的答案有效。

但不确定如何进行此更改

在当前情况下,我们正在查找列 condition=yes

之后的行中的特定条件

有没有办法查看当前行之后的行,而不是查看之前的行?

如果我没听错,你可以使用 window 函数:

select t.*,
    case when other_condition = 'x'
        and max(condition) over(partition by episode_start, episode_end order by index) is not null
    then 'flag'
    end as newcol
from mytable t

一旦遇到非null condition,此标记所有后续行都具有相同的episode_startepisode_end以及非null other_condition.

中的值