k_suchdeva
asked on
selecting first and last records from a result set
how do i select top x , bottom x rows in a select query.
i mean something like
select * from <blah blah> where <blah blah> first 10;
select * from <blah blah> where <blah blah> last 10;
i mean something like
select * from <blah blah> where <blah blah> first 10;
select * from <blah blah> where <blah blah> last 10;
ASKER
I have tried these two already.
SOrry forgot to mention my datbaase is oracle and i am trying it from sqlplus.
select top 10 * from <table>;
select top 10 * from <table>
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
set rowcount 10
SP2-0158: unknown SET option "rowcount"
SOrry forgot to mention my datbaase is oracle and i am trying it from sqlplus.
select top 10 * from <table>;
select top 10 * from <table>
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
set rowcount 10
SP2-0158: unknown SET option "rowcount"
Check out this:
select * from yourtable where rownum <=10 order by somefield asc
select * from yourtable where rownum <=10 order by somefield desc
!!!! UNTESTED !!!!
select * from yourtable where rownum <=10 order by somefield asc
select * from yourtable where rownum <=10 order by somefield desc
!!!! UNTESTED !!!!
k_suchdeva,
As long as you are on Oracle 8i or later, you can use the analytic functions (angelll's solution won't work since rownum is assigned prior to the sort):
Top 10:
select * from
(select <blah column 1>, <blah column 2>, <etc.>,
row_number() over (order by <blah col 1>,
<blah col2>) ranking
from <blah blah>)
where ranking <= 10;
Bottom 10 (just reverse the order by):
select * from
(select <blah column 1>, <blah column 2>, <etc.>,
row_number() over (order by <blah col 1> desc,
<blah col2> desc) ranking
from <blah blah>)
where ranking <= 10;
you can also sort the whole thing (takes longer) instead of using these functions, and put it in an inline view:
select * from (select * from <blah blah> order by <blah columns>)
where rownum <= 10;
and the reverse (putting desc on the columns in the order by) for the bottom 10.
Good luck!
As long as you are on Oracle 8i or later, you can use the analytic functions (angelll's solution won't work since rownum is assigned prior to the sort):
Top 10:
select * from
(select <blah column 1>, <blah column 2>, <etc.>,
row_number() over (order by <blah col 1>,
<blah col2>) ranking
from <blah blah>)
where ranking <= 10;
Bottom 10 (just reverse the order by):
select * from
(select <blah column 1>, <blah column 2>, <etc.>,
row_number() over (order by <blah col 1> desc,
<blah col2> desc) ranking
from <blah blah>)
where ranking <= 10;
you can also sort the whole thing (takes longer) instead of using these functions, and put it in an inline view:
select * from (select * from <blah blah> order by <blah columns>)
where rownum <= 10;
and the reverse (putting desc on the columns in the order by) for the bottom 10.
Good luck!
ASKER
Strangely
select * from mr_orders where rownum <=1;
works
while
select * from mr_orders where ranking <=1;
does not
So who wants the points ?
select * from mr_orders where rownum <=1;
works
while
select * from mr_orders where ranking <=1;
does not
So who wants the points ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi DrSQL
Got it !!
This is one thing i love so much about this site.
People are just passionate about explaining the problem and you get you get exactly the answer you want.
Thanks for ur effort.
Got it !!
This is one thing i love so much about this site.
People are just passionate about explaining the problem and you get you get exactly the answer you want.
Thanks for ur effort.
Here is the suggestion for SQL Server:
select top 10 * from yourtable order by somefield asc
union all
select top 10 * from yourtable order by somefield desc
or
set rowcount 10
select * from yourtable order by somefield asc
union all
select * from yourtable order by somefield desc
set rowcount 0
CHeers