SQL返回特定行的rownum? (使用Oracle数据库)
在 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
但这效果不是很好(无论我匹配哪个dog.id,它都会返回1)。 我正期待着回来 3。
谢谢你的帮助!
注释
http://www. oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
我很确定我不需要使用 rowid
In Oracle 10g, I have this SQL:
select dog.id as dogId from CANINES dog order by dog.codename asc
which returns:
id
--
204
203
206
923
I want to extend this query to determine the oracle rownum of a dog.id in this resultset.
I have tried
select rownum from
(select dog.id as dogId from CANINES dog order by dog.codename asc)
where dog.id=206
But this does not work out very well (it returns 1 no matter which dog.id I match on). I was expecting to get back 3.
Thanks for your help!
Notes
http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
I am pretty sure I do not need to use rowid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑您想要的是使用分析函数(RANK、
DENSE_RANK
或ROW_NUMBER
),即如果 CANINES 表中的 ID 列不唯一,则 RANK, < code>DENSE_RANK 和
ROW_NUMBER
)会以不同方式对待关系。如果您只想使用 ROWNUM 来执行此操作,
I suspect what you want is to use an analytic function (RANK,
DENSE_RANK
, orROW_NUMBER
), i.e.If the ID column in the CANINES table were not unique, RANK,
DENSE_RANK
, andROW_NUMBER
) would treat ties differently.If you want to do this solely with ROWNUM,
如果您想要表中每一行的唯一标识符,则需要 ROWID,而不是 ROWNUM。
ROWNUM 是一个伪列,每次执行一段 SQL 时都会发生变化(它是在查询时计算出来的)
If you're after the unique identifier of each row in the table you need ROWID, not ROWNUM.
ROWNUM is a pseudocolumn that can change each time a bit of SQL is executed (it's worked out at query time)
看看这是否适合您:
答案
结果
DDL
See if this works for you:
Answer
Results
DDL
为了实现这一点,最好更改表并添加序列。 如果您打算删除行,这可能会变得棘手。 在这种情况下,也许更好的做法是使用状态列或开始结束日期主题来决定哪些行处于活动状态并且应该返回。
In order to accomplish this, it would be best to alter the table and add a sequence. This could get sticky if you intend to delete rows. Where, perhaps a better practice would be to use a status column or and start-end-date motif to decide which rows are active and should be returned.