Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2011
    Posts
    3

    Unanswered: SQL Question - Very new to DB2

    I am currently working on a multiple query assignment that is asking for the second to least value in a field. How would I go about this?

  2. #2
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    There are several variations on this, but here is one using the DB2 sample database:

    with T (emp_salary, row_num)
    as (select salary, row_number() over (order by salary asc) from employee)
    select emp_salary from T where row_num = 2"
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  3. #3
    Join Date
    Oct 2011
    Posts
    3

    Unhappy

    Hmmm...I'm afraid that's a little over my head, but I have realized I have a bigger problem that needs fixing before I move onto that. Thanks for the help tho.

    I am trying to display rows that are only less than the average of one column divided by another.

    So:

    SELECT COLUMN A, COLUMN B, COLUMN C / COLUMN D
    FROM TABLE
    WHERE (COLUMN C / COLUMN D) < AVG (COLUMN C / COLUMN D)

    is what I have right now, though I realize the MIN (COLUMN C / COLUMN D) is impossible. How would you perform this type of query?

    Thanks in advance.

  4. #4
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    select * from employee
    where bonus / salary < (select avg(bonus / salary) from employee)
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  5. #5
    Join Date
    Oct 2011
    Posts
    3

    Talking

    Thank you so much Marcus. Much appreciated!

Posting Permissions

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