Oracle SQL 选择具有组设置值的两行之间的行

问题描述 投票:0回答:1

我有一张如下表:

enter image description here

对于每个组,我想选择停止 20231 和 21567 之间的行。因此结果表应如下所示:

enter image description here

如何编写 SQL 查询来在 Oracle 中实现此目的?

我试过这个:

选择组、排序顺序、停止编号 从表 在哪里 Sort_Sequence >=(从 Stop_number = 20231 的表中选择 Sort_Sequence) 和 Sort_Sequence <= (select Sort_Sequence from table where stop_number = 21567)

我遇到了这个错误: ORA-01427: 单行子查询返回多于一行

sql oracle
1个回答
0
投票

正如错误消息所示,子查询之一(或两者)返回多个记录。换句话说,您实际上有多个具有相同

Stop_number
的记录。根据您的示例数据,这似乎是正常的。

您应该在子查询中包含“组”。也许使用相关查询:

SELECT Groups, Sort_Sequence, Stop_number
  FROM table t
  WHERE Sort_Sequence >= (select Sort_Sequence from table where Stop_number = 20231 and Groups = t.Groups)
    AND Sort_Sequence <= (select Sort_Sequence from table where stop_number = 21567 and Groups = t.Groups)
© www.soinside.com 2019 - 2024. All rights reserved.