oracle中的第一个负数行

qmb5sa22  于 2021-07-24  发布在  Java
关注(0)|答案(3)|浏览(273)

如何在oracle中找到负值开始的第一行?下面是一个例子。

77
74
67
56
42
24
20
19
-17
-28
-31
-36

我想读第17行并对该行进行一些操作。
非常感谢您的帮助。谢谢

jchrr9hc

jchrr9hc1#

如果需要完整的行,可以使用:

select t.*
from t
where n < 0
order by n asc
fetch first 1 row only;

如果有另一列指定行的顺序,则:

select t.*
from t
where n < 0
order by <ordering col> asc
fetch first 1 row only;
3zwtqj6y

3zwtqj6y2#

要选择最大负数,可以执行以下操作:

select max(n)
from t
where n < 0

请记住,在关系数据库表中,行没有固有的顺序。因此,在没有排序标准的情况下,不存在“负值开始的第一行”这样的东西。

deikduxw

deikduxw3#

假设您有一个用于排序的列,它定义了行的顺序,它可以如下所示:

with t as (
select 77  a, 1  row_order  from dual union all
select 74  a, 2 row_order from dual union all
select 67  a, 3 row_order from dual union all
select 56  a, 4 row_order from dual union all
select 42  a, 5 row_order from dual union all
select 24  a, 6 row_order from dual union all
select 20  a, 7 row_order from dual union all
select 19  a, 8 row_order from dual union all
select -17 a, 9 row_order from dual union all
select -28 a, 10 row_order from dual union all
select -31 a, 11 row_order from dual union all
select -36 a, 12 row_order from dual
), t1 as (
select a, row_number() over (partition by case when a < 0 then 0 else 1 end order by row_order) rn from t
)
select * from t1 where rn = 1 and a < 0;

它使用一个窗口函数来确定第一行(这里是正数a和负数a),然后它选择遇到的第一行是负数。

相关问题