to get the row that appears first or Last

  • hey all

    i got a pblm.i am unable 2 explain that. so i will give an example.

    Table:

    name     mark

    abc         10

    cde          20

    def          30

    gfh          40

    xyz          30

    www        20

     

    now the output i require is:// first occurance of mark=20.(i presume this could be done using Top key word?)

    name  mark

    cde     20

    then 2nd output i require is//last occurance of mark=20

    name  mark

    www   20

    Thanx

    Rajiv.

  • If 'first' = 'min' and 'last' = 'max', then this will do it:

    select min(name) as 'first', max(name) as 'last'

    from

    where mark = 20

    Regards

    Phil

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Try this examples

     

    select

     *

    from

     employees

    where

     employeeid = (select min(Employeeid) from employees) or

            employeeid = (select max(Employeeid) from employees)

    or

    select

     *

    from

     employees

    where

     employeeid in

            ((select min(Employeeid) from employees),

                           (select max(Employeeid) from employees))

     

  • I have to ask the question - why do you care which row contains the first occurance of a value? 

    in a relational database the order of rows in a table is supposed to be irrelevant.  You must use ORDER BY to retrieve rows in any meaningful order.

    -Ken

  • hey all

    thanx for all ur help. answering to ken's question-->i wanna know how to select a row based on rownumber.

    is there any equivalent to rownum and rowid in oracle

     

    thanx

    Rajiv.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply