Sql 是否可以在选定行之后看到上一行?

Sql 是否可以在选定行之后看到上一行?,sql,oracle,Sql,Oracle,我创建了一个这样的表 create table numbers( first int ... second int... third int ... fourth .... fifth int.. sixt int ... id primary key.. ...); 每个sextain都有一个标识 我想看看我选择的那一行的前一行 范例 作为输出(我们将得到所有数字,第一个和第二个是6和54,id为) 我也想看看第9排和第519排,有没有办法看看这些?我知道我 可以 select

我创建了一个这样的表

create table numbers(

first int ...

second int...

third int ...

fourth ....

fifth int..

sixt int ...

id primary key..
...);
每个sextain都有一个标识

我想看看我选择的那一行的前一行 范例

作为输出(我们将得到所有数字,第一个和第二个是6和54,id为)

我也想看看第9排和第519排,有没有办法看看这些?我知道我 可以

select * from numbers where id=9;
select * from nbumbers where id= 519;
但我有50-60排这是不可能的 逐个进行。

您可能需要:

select
  n.*
from (
  select * from numbers where first = 6 and second = 54
) x
join numbers n on n.id = x.id - 1
您可能想要:

select
  n.*
from (
  select * from numbers where first = 6 and second = 54
) x
join numbers n on n.id = x.id - 1
你可以用

SELECT * FROM NUMBERS WHERE ID IN 
(SELECT ID -1 FROM NUMBERS WHERE first = 6 and second = 54);
你可以用

SELECT * FROM NUMBERS WHERE ID IN 
(SELECT ID -1 FROM NUMBERS WHERE first = 6 and second = 54);
您可以使用
lead()

另一种方法将使用累积总和:

select n.*
from (select n.*,
             max(case when first = 6 and second = 54 then id end) over
                 (order by id
                  rows between unbounded preceding and 1 preceding
                 ) as prev_id
      from numbers n
     ) n
where prev_id = id;
您可以使用
lead()

另一种方法将使用累积总和:

select n.*
from (select n.*,
             max(case when first = 6 and second = 54 then id end) over
                 (order by id
                  rows between unbounded preceding and 1 preceding
                 ) as prev_id
      from numbers n
     ) n
where prev_id = id;

我删除了所有三个数据库标记。请标记您使用的数据库。我删除了所有三个数据库标记。请给你用的贴上标签。谢谢!!!!!!!!!!!!!!!!!!!!这是一个巨大的思考方式。我需要多练习!!谢谢这是一个巨大的思考方式。我需要多练习!!很好,我不认为我需要加入。。。这是一个聪明的思考方式我不认为我可以使用加入。。。这是一种聪明的思考方式