Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2014
    Posts
    1

    Unanswered: Second max (oracle 10g)

    i have tried to fetch second max value from a list of values using rownum, row_number, but failed. The query is as follows:

    select Sal from EmpTab where EmpId in (2,4,7) and rownum = 2 order by Sal desc;

    the use of rownum doesn't select any rows, row_number either doesn't work.

    plz help me how to fetch second max from a list of values on oracle 10g.

    thank you,
    Lynn321

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    Nov 2003
    Posts
    2,934
    Provided Answers: 12
    Quote Originally Posted by Lynn321 View Post
    row_number either doesn't work.
    I don't believe that:

    Code:
    select empid, sal,
    from (
       select empid, 
              sal,
              row_number() over (order by sal desc) as rn
      from empTab
      where empid in (2,4,7)
    ) t
    where rn = 2;
    If you want to include employees with the same salary, you need to use dense_rank()
    Code:
    select empid, sal,
    from (
       select empid, 
              sal,
              dense_rank() over (order by sal desc) as rnk
      from empTab
      where empid in (2,4,7)
    ) t
    where rnk = 2;

    plz help me
    I don't understand what German ZIP codes have to do with this (PLZ = ZIP code)
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags: http://www.dbforums.com/misc.php?do=bbcode#code

    Tips for good questions:

    http://tkyte.blogspot.de/2005/06/how...questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •