If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > SQL Question - Very new to DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-12-11, 00:04
thewaffle101 thewaffle101 is offline
Registered User
 
Join Date: Oct 2011
Posts: 3
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?
Reply With Quote
  #2 (permalink)  
Old 10-12-11, 00:23
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
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
Reply With Quote
  #3 (permalink)  
Old 10-12-11, 01:17
thewaffle101 thewaffle101 is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 10-12-11, 02:19
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
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
Reply With Quote
  #5 (permalink)  
Old 10-12-11, 11:38
thewaffle101 thewaffle101 is offline
Registered User
 
Join Date: Oct 2011
Posts: 3
Talking

Thank you so much Marcus. Much appreciated!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On