Link to home
Start Free TrialLog in
Avatar of cbridgman
cbridgmanFlag for United States of America

asked on

SQL To Find 1st highest, 2nd highest, 3rd highest, etc. records

I have a table with the following columns: assetnumber, assetdescription, and assetprice. Assetnumber is unique. To find the row with the highest assetprice and include all 3 columns in the result set I would run a query like the following:

select assetnumber, assetdescription, and assetprice from assettable
where assetprice = (select max(assetprice) from asset)

How would I find the row with the 2nd highest price? How about the 3rd, then the 4th .....
Avatar of cbridgman
cbridgman
Flag of United States of America image

ASKER

I made an error in my query. It should have read:

select assetnumber, assetdescription, assetprice from assettable
where assetprice = (select max(assetprice) from asset)
Avatar of Gerwin Jansen
Something like:
  select assetnumber, assetdescription, assetprice from assettable
  where assetprice = (select assetprice from asset order by assetprice desc where rownum=2);
Why don't you order by that column

select  assetnumber, assetdescription, assetprice from assettable
order by assetprice desc
You have selected both MSSQL and Oracle - do you need a generic or specific solution?
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

For Oracle try:

select rn, assetnumber, assetdescription, assetprice from (
select assetnumber, assetdescription, assetprice,
row_number() over(order by assetprice desc) rn
 from assettable
)
where rn<=4
order by rn
/
:( the 'where rownum=2' is not working, there is a somewhat more cryptic way to get the 2nd from the list:

select assetnumber, assetdescription, assetprice from assettable
where assetprice =( select assetprice from ( select assetprice from ( select assetprice from asset order by assetprice desc) whererownum < 3 order by 1 ) where rownum = 1);
I think what I would like is an Oracle version of the script and an SQL Server version. If they are the same, that's even better. With regard to the result set, I want the result set to contain exactly one row. The first query would ask for the row with the highest asset price. The second query would ask for the row with the second highest asset price. The 3rd query would ask for the row with the 3rd highest asset price. Etc.

I hope that helps.
slightwv's query should work with both
SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help. Everything works like a charm