Results 1 to 3 of 3

Thread: DB2 query

  1. #1
    Junior Member
    Join Date
    Feb 2007
    Answers
    4

    DB2 query

    How to retrieve the third maximum value from a column ?
    For Ex: If i have emp-id as one column with values 111,458,785,896,274,698 etc, using SELECT MAX(EMP-ID) retrieves the value "896". But i need to retrieve third maximum value ie., 698 incase of above example. How to write query for this ?


  2. #2
    Contributing Member
    Join Date
    Sep 2007
    Answers
    44

    Re: DB2 query

    Query to select top nth record:

    consider the table name as emp,then

    select empid
    from emp e1
    where ( n =
    ( select count ( distinct ( e2.empid ) )
    from emp e2
    where e2.empid >=e1.empid))

    replace n by 3 to get the third maximum


  3. #3
    Junior Member
    Join Date
    Feb 2007
    Answers
    7

    Re: DB2 query

    select max(empid) from table1
    where empid not in (select distinct(empid) from table1
    order by empid desc
    fetch first 2 rows only)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact