SQL分段排名操作

时间:2019-05-08 15:26:22

标签: sql transactions ranking audit-trail dense-rank

我愿意找到排在Text = E的(最新)行之后的行。

这就像一个事务日志,在这里我想查找在Text等于E之后恰好发生的情况。Date列可能令人困惑,请仅尊重RANK列。

示例: 我想从下表中获取第6行(或6和7)和14行(或14和15):

非常感谢。

table

1 个答案:

答案 0 :(得分:0)

相关子查询是一种简单的解决方案:

select t.*
from t
where t.date > (select max(t2.date) from t t2 where t2.id = t.id and t2.text = 'E');

如果要使用窗口功能:

select t.*
from (select t.*, max(case when text = 'E' then date end) as e_date
      from t
     ) t
where date > e_date;