带分组依据的oracle滞后函数

Max*_*eue 2 sql database oracle

我有一个查询,它忽略从前一个值增加的值。例如,取下表:

col1    col2    col3
5       1       A
4       2       A
6       3       A
9       4       B
8       5       B
10      6       B
Run Code Online (Sandbox Code Playgroud)

现在进行以下查询:

select col1
from (select col1, lag(col1) over (order by col2) as prev_value
  from test
 ) 
where prev_value is null or prev_value > col1;
Run Code Online (Sandbox Code Playgroud)

现在,查询将按预期工作,因为它会忽略col1 = 6,9和10的列,因为先前的值较小。现在,我要解决的问题是要按小组进行此操作。所以我只希望它忽略由col3分组的先前值。所以我确实希望忽略6和10,但不要忽略9,因为当按col3 9分组时,将没有先前的值。

任何帮助将不胜感激

Gor*_*off 5

我想你只想要partition by

select col1
from (select col1, lag(col1) over (partition by col3 order by col2) as prev_value
      from test
     ) 
where prev_value is null or prev_value > col1;
Run Code Online (Sandbox Code Playgroud)