不使用多个内联选择的Oracle SQL查询
环境:甲骨文12c 我有一个名为:MY_TAB的表,其中包含以下示例数据:不使用多个内联选择的Oracle SQL查询,sql,oracle,oracle12c,Sql,Oracle,Oracle12c,环境:甲骨文12c 我有一个名为:MY_TAB的表,其中包含以下示例数据: CODE KEY_ID ------------ -------- 2000 95 1055 96 2000 97 使用上述示例表数据,我需要获取最小代码值,在本例中为1055,连同该最小代码值旁边的KEY_ID,即96,并在另一个查询中使用它: select * from another_table where code = minimum COD
CODE KEY_ID
------------ --------
2000 95
1055 96
2000 97
使用上述示例表数据,我需要获取最小代码值,在本例中为1055,连同该最小代码值旁边的KEY_ID,即96,并在另一个查询中使用它:
select *
from another_table
where code = minimum CODE value from MY_TAB
and key_id = KEY_ID alongside the minimum CODE value
我想用这样的东西:
where code = (select min(CODE) from MY_TAB where .....) 1055
and key_id = select KEY_ID from MY_TAB for MIN(CODE) 96
不确定在没有多个嵌套内联选择的情况下处理此查询的最佳方式。我建议:
select code, key_id
from my_tab
order by code
fetch first 1 row only
您可以在查询中使用它作为:
where (t.code, t.key_id) in (select code, key_id
from my_tab
order by code
fetch first 1 row only
)
旧版Oracle中的另一个替代方案是:
select min(code) as code,
min(key_id) keep (dense_rank first order by code) as key_id
from my_tab;
查询返回正确的信息,但使用
仅获取前1行
时,这将始终返回按代码排序的最小b/c
-正确?@tonyf。对orderby
是在fetch
之前处理的。太好了-我采用了fetch
方法,这似乎是rownum
的替代方法。