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
See if this works for you:
Answer
Results
DDL
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,
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)
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.