Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LIMIT equivalent 1

Status
Not open for further replies.

TGJ

Programmer
Jan 31, 2005
64
CA
I am wondering if there is an oracle equivalent to the mysql LIMIT function.

This is what I am trying to achieve:

SELECT *
FROM data
WHERE disabled_date > last_day_empl_paid_date
ORDER BY sequence_num DESC
Limit 1
 
TGJ,

Are you simply looking to see all data on the row with the highest-valued "sequence_num" whose disabled_date > last_day_empl_paid_date? If so, then there are multiple methods to achieve this:

Sample data:
Code:
select * from data;

DISABLED_ LAST_DAY_ SEQUENCE_NUM
--------- --------- ------------
12-AUG-05 10-AUG-05           17
12-AUG-05 10-AUG-05           16
12-AUG-05 04-AUG-05           31
13-AUG-05 14-AUG-05           55

Method 1:
Code:
select * from data
where sequence_Num in (select max(sequence_num) from data
                        where disabled_date > last_day_empl_paid_date)
  and disabled_date > last_day_empl_paid_date;

DISABLED_ LAST_DAY_ SEQUENCE_NUM
--------- --------- ------------
12-AUG-05 04-AUG-05           31

Method 2:
Code:
select *
from (select * from data
       where disabled_date > last_day_empl_paid_date
       ORDER BY sequence_num DESC)
where rownum = 1;

DISABLED_ LAST_DAY_ SEQUENCE_NUM
--------- --------- ------------
12-AUG-05 04-AUG-05           31

I like the second method since if you want to see the top two rows, then you can easily adjust the code to make that happen:
Code:
select *
from (select * from data
       where disabled_date > last_day_empl_paid_date
       ORDER BY sequence_num DESC)
where rownum <= 2;

DISABLED_ LAST_DAY_ SEQUENCE_NUM
--------- --------- ------------
12-AUG-05 04-AUG-05           31
12-AUG-05 10-AUG-05           17
Let us know if you like either one of these options.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[ Providing low-cost remote Database Admin services]
Click here to join Utah Oracle Users Group on Tek-Tips if you use Oracle in Utah USA.
 
Method 1 is perfect, thanx =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top