在Oracle 10g中,我有这个SQL:
select dog.id as dogId from CANINES dog order by dog.codename asc
返回:
id -- 204 203 206 923
我想扩展此查询以确定此结果集中dog.id的oracle rownum.
我试过了
select rownum from (select dog.id as dogId from CANINES dog order by dog.codename asc) where dog.id=206
但这不是很好(无论哪匹狗都会返回1.我匹配).我本以期待回来3.
谢谢你的帮助!
笔记
http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
我很确定我不需要使用rowid
我怀疑你想要的是使用分析函数(RANK DENSE_RANK
,,或ROW_NUMBER
),即
SELECT rnk FROM (select dog.id as dogId, ROW_NUMBER() OVER( ORDER BY dog.codename ASC ) rnk from CANINES dog ) WHERE dogId = 206
如果CANINES表中的ID列不是唯一的,则RANK DENSE_RANK
,和ROW_NUMBER
)将以不同方式处理关系.
如果你想单独使用ROWNUM,
SELECT rn FROM ( SELECT dogId, rownum rn FROM (select dog.id as dogId from CANINES dog order by dog.codename ASC) inner ) middle WHERE dogId = 206