gpt4 book ai didi

sql - 具有分组依据的oracle滞后函数

转载 作者:搜寻专家 更新时间:2023-10-30 20:03:54 24 4
gpt4 key购买 nike

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

col1    col2    col3
5 1 A
4 2 A
6 3 A
9 4 B
8 5 B
10 6 B

现在进行以下查询:

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;

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

任何帮助将不胜感激

最佳答案

我想你只是想要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;

关于sql - 具有分组依据的oracle滞后函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45698598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com