Question : Getting the row number of a record

I will need to write a subquery which
will return the row position or row number
of a particular record

servwait_pk is the primary key in this table
and I will like to return either of  the following
at any time

if student_fk = 39170 query should return
row position 2

if student_fk = 40259 query should return
row position 3

if student_fk = 43515 query should return
row position 13

please take a look at the image. This is the sample table i am working with

Attachments:
 
sampleData for table
sampleData for table
 

Answer : Getting the row number of a record

In my query, since there is no filter it is returning all records. so add a WHERE condition something like shown below. (eg:- somecolumn=2324)

select rowno from
(select row_number() over(order by servwait_pk) rowno, *
from yourtable where somecolumn=somevalue) a
where student_fk = 39170

Let me know
Raj
Random Solutions  
 
programming4us programming4us