sql - Retrieve only the first row with ORDER BY -


i have table contains salary increase history (oracle) emp_id - employee identification, inc_date - date salary changed , inc_amount - amount of change in salary. inc_amount last inc_date.

 emp_pay_inc:    ============================== emp_id | inc_date | inc_amount ============================== 625    | 1/1/2002 | 0 625    | 5/6/2003 | 12000 625    | 1/7/2004 | 35000 625    | 8/1/2009 | -5000 

pseudo code query do:

select epi.inc_amt   emp_pay_inc epi  epi.inc_date = max(epi.inc_date) -- know won't work, illustration 

what have tried (i didn't want use sub-query in event there duplicate inc_date same emp_id:

select epi.inc_amt   emp_pay_inc epi  rownum = 1   order epi.inc_date 

but doesn't work. returns inc_amount 0 inc_date 1/1/2002. apparently oracle stores rownum appear in original table not data set returned query.

you should able use subquery this:

select *   (select epi.inc_amt emp_pay_inc epi order epi.inc_date desc) rownum = 1; 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -