问题描述
仅在列紧随其后的行中具有特定值的情况下,我才选择列。
使用以下数据框的示例:
Date ID Symbol
2020-06-05 C AAPL
2020-06-05 U AAPL
2020-06-06 R AAPL
我想对它进行编码,所以我只返回第二行,因为它在“ ID”行中紧跟着“ R”
目前,我正在使用以下代码结构预先检查值。我想知道是否可以进行类似的更改以使其潜在地起作用。
select t.* from table t
where not exists (
select 1 from table
where id < t.id and val > t.val)
谢谢
解决方法
您在数据中拥有联系。让我假设日期确实有时间成分,所以这不是问题。
在这种情况下,您可以使用lead()
:
select t.*
from (select t.*,lead(id) over (partition by symbol order by date) as next_id
from t
) t
where next_id = 'R';