Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 28th, 2004, 04:22 AM
rajanyprasad rajanyprasad is offline
Nexus
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Bangalore, India
Posts: 7 rajanyprasad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to rajanyprasad
Question PL/SQL query

How to find the nth highest salary in a table ?

Please suggest.

Reply With Quote
  #2  
Old September 30th, 2004, 07:08 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
any restrictions? Nice question.

Reply With Quote
  #3  
Old September 30th, 2004, 07:45 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
I came up w/this...I'm a SQL Server guy but liked the ? so I took a stab...this is the main idea though...
Code:
select	max(salary) from
	(
	 select	top 20 * from Employee
	 order	by salary
	); 

...I was able to put it in t-SQL to make it more dynamic w/this...(I'm not sure what Oracle's = is to t-SQL_
Code:
Declare @nth as int
set	@nth = 10

declare @qry as varchar(200)

set	@qry = 'select	max(salary) from
		(
		 select	top ' + cast(@nth as varchar(10)) + ' * from Employee
		 order	by salary
		) as A'
exec (@qry)

Maybe you can take that approach and throw Oracle syntax @ it to make it work. If it's not a valid approach let me know what the guidelines are if you have any.

Last edited by Username=NULL : September 30th, 2004 at 10:53 PM.

Reply With Quote
  #4  
Old September 30th, 2004, 08:48 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 6
make your life easy, look at the following query

SELECT *
FROM (SELECT * FROM emp
ORDER BY sal DESC)
WHERE rownum<21 -- show the top 20 highest salaries
/

Reply With Quote
  #5  
Old October 1st, 2004, 01:29 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
shafique, can you show me a more dynamic solution? Not trying to argue, we basically use the same approach, but I'd like to see how Oracle's equivalent to MS SQL's T-Sql looks. Just now getting into Oracle stuff so I'm interested, thx.

Last edited by Username=NULL : October 1st, 2004 at 02:43 PM.

Reply With Quote
  #6  
Old October 1st, 2004, 02:04 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 6
Yes, sure, for dynamic purpose you need to use bind variable that is defined in sql by using & sign with any variable name so when you run the query, oracle will prompt you to pass values at runtime. look at the following code :

SELECT *
FROM (SELECT * FROM emp
ORDER BY sal DESC)
WHERE rownum<&nth

If the column data type is character then enclose this variable into single quote, like that:

WHERE rownum<'&nth'

If you are looking for more information about the bind variable, please read the oracle sql documentation, provided by the oracle with no cost, just need to register at www.oracle.com no charge for registration either. Or visit http://tahiti.oracle.com (still requires registration at oracle)

Reply With Quote
  #7  
Old October 1st, 2004, 02:45 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
great, thx man.

Reply With Quote
  #8  
Old October 4th, 2004, 04:17 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
actually shafique, our approach has a hole. Say we want the 3rd highest salary and we have a table with this data....
Code:
EmployeeID	Salary
----------	------
24		500
38		500	
94		450
23		500
31		900	
43		750

750 is the 3rd highest salary but our queries would return 500 as the 3rd highest.
Code:
EmployeeID	Salary
----------	------
24		500
38		500	
23		500

We should've used a select distinct somewhere maybe, not sure of the syntax off the bat.

Reply With Quote
  #9  
Old October 4th, 2004, 07:03 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 6
You are right, my previous query could return duplicate values and we can eliminate those value using DISTINCT keyword.

As long as i understand your question so you are looking for a specific value(s) return by the result in specific order, you have to do a little bit more effort and it might not easy for you to write such a query. fro example the following query will return the employee(s) information whose salary is 3rd highest in the company:

select e.empno,e.ename,e.sal,e.deptno,e.job
from (select rownum row_count, sal
from ( select distinct sal
from emp
order by sal desc)) b, emp e
where b.row_count = 2
and b.sal = e.sal

the following query will return the employees information their salaries are positioned 1st, 2nd and 5th on the payroll chart.

select e.empno,e.ename,e.sal,e.deptno,e.job
from (select rownum row_count, sal
from ( select distinct sal
from emp
order by sal desc)) b, emp e
where b.row_count in (1, 2, 5)
and b.sal = e.sal
order by e.sal desc

Reply With Quote
  #10  
Old October 15th, 2004, 02:51 AM
rajanyprasad rajanyprasad is offline
Nexus
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Bangalore, India
Posts: 7 rajanyprasad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to rajanyprasad
PL/SQL query

Guys !!

Thanks a lot for the inputs. This is very informative !!!

Thanks again.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > PL/SQL query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT