MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Select top 3 salaries from Employee table

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12366
Topics: 75
Location: San Jose

PostPosted: Sun Dec 01, 2002 10:57 pm    Post subject: Select top 3 salaries from Employee table Reply with quote

Q.I have an 'employee' table with 3 columns emp-id, emp-name and emp-salary. I want to select first 3 employees in the order of their salary.The trick is to get the results without using a cursor and with a 'single' query.

Answer:

Code:

SELECT * FROM EMP_TABLE                 
         WHERE SAL >= (SELECT MAX(SAL) FROM EMP_TABLE             
                       WHERE SAL < (SELECT MAX(SAL) FROM EMP_TABLE   
                                    WHERE SAL < (SELECT MAX(SAL)   
                                                 FROM EMP_TABLE)))
        ORDER BY SAL DESC       
         ;                         



or

Code:

SELECT * FROM                                                       
       EMP_TABLE                                                     
       WHERE                                                         
       SAL = (SELECT MAX(SAL) FROM EMP_TABLE)

       UNION                                                     
SELECT * FROM                                                       
       EMP_TABLE                                                   
       WHERE                                                         
       SAL = (SELECT MAX(SAL) FROM EMP_TABLE                     
              WHERE SAL < (SELECT MAX(SAL) FROM EMP_TABLE))   
       UNION                                                         
SELECT * FROM                                                       
       EMP_TABLE                                                     
       WHERE                                                         
       SAL = (SELECT MAX(SAL) FROM EMP_TABLE                   
              WHERE SAL < (SELECT MAX(SAL) FROM EMP_TABLE     
              WHERE SAL < (SELECT MAX(SAL) FROM EMP_TABLE)))
       ORDER BY SAL DESC;                                           



PS: UNION clause will remove any duplicates.If you want the duplicate values use UNION ALL

For a generic selection of first 'N' rows

Code:

SELECT *
       FROM
       EMP_TABLE X 
       WHERE n > ( SELECT COUNT(*) FROM EMP_TABLE
                   WHERE SAL > X.SAL )
       ORDER BY X.SAL DESC
       ; 


Last edited by kolusu on Wed Oct 29, 2003 10:05 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vallishar
Beginner


Joined: 17 Dec 2002
Posts: 53
Topics: 14
Location: BengaLuru

PostPosted: Mon Feb 10, 2003 12:32 pm    Post subject: Reply with quote

Hi

Im doubtful of the order of execution in the first query. Could you pls explain?
_________________
If you're not failing every now and again, it's a sign you're not doing anything very innovative.
Back to top
View user's profile Send private message Yahoo Messenger
gotluru
Beginner


Joined: 17 Dec 2002
Posts: 29
Topics: 6

PostPosted: Mon Feb 10, 2003 2:18 pm    Post subject: Reply with quote

Hi Kolusu,

Did this select works for your question?

SELECT *
FROM
EMP_TABLE X
ORDER BY X.SAL DESC
FETCH FIRST 3 ROWS ONLY;

Prasad
Back to top
View user's profile Send private message
vallishar
Beginner


Joined: 17 Dec 2002
Posts: 53
Topics: 14
Location: BengaLuru

PostPosted: Mon Feb 10, 2003 2:50 pm    Post subject: Reply with quote

The Query with FIRST FETCH N ROWS gives the expected result too.

But the query
Quote:

SELECT *
FROM
EMP_TABLE X
WHERE n > ( SELECT COUNT(*) FROM EMP_TABLE
WHERE SAL > X.SAL )
ORDER BY X.SAL DESC
;


Doesn't seem to be doing the same.
_________________
If you're not failing every now and again, it's a sign you're not doing anything very innovative.
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12366
Topics: 75
Location: San Jose

PostPosted: Mon Feb 10, 2003 6:59 pm    Post subject: Reply with quote

vallish,

can you elaborate as to what you meant by "it doesn't seem to be the doing the same"?

Gotluru: your select statement with FETCH FIRST 3 ROWS also works.It is a new feature of db2 v7. Also it wouldn't elimintate duplicates.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vallishar
Beginner


Joined: 17 Dec 2002
Posts: 53
Topics: 14
Location: BengaLuru

PostPosted: Tue Feb 11, 2003 10:26 am    Post subject: Reply with quote

Kolusu,

I tried to execute the SQL
Quote:

SELECT *
FROM
EMP_TABLE X
WHERE n > ( SELECT COUNT(*) FROM EMP_TABLE
WHERE SAL > X.SAL )
ORDER BY X.SAL DESC
;



With the value for N = 3. The table contains 10 rows. This query is returning me all the 10 rows. Am I doing anything wrong?
_________________
If you're not failing every now and again, it's a sign you're not doing anything very innovative.
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12366
Topics: 75
Location: San Jose

PostPosted: Tue Feb 11, 2003 9:44 pm    Post subject: Reply with quote

vallishar,

The query works fine for me , can you post your sql so that I can take a look at it?

Thanks

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vallishar
Beginner


Joined: 17 Dec 2002
Posts: 53
Topics: 14
Location: BengaLuru

PostPosted: Thu Feb 13, 2003 11:19 am    Post subject: Reply with quote

Hi Kolusu,

The query gives the correct result for the distinct values of the selected field., in this case emp_sal. When there are duplicate rows, the number of rows selected may not be the same as expected.

I had overlooked the result of my query. Aplogies.

Thanks
_________________
If you're not failing every now and again, it's a sign you're not doing anything very innovative.
Back to top
View user's profile Send private message Yahoo Messenger
Premkumar
Moderator


Joined: 28 Nov 2002
Posts: 77
Topics: 7
Location: Chennai, India

PostPosted: Thu Feb 13, 2003 11:48 pm    Post subject: Reply with quote

That is a query to select 'Top n, with ties for the last place' and NOT 'First n'.
Back to top
View user's profile Send private message Send e-mail
Glenn
Beginner


Joined: 23 Mar 2003
Posts: 56
Topics: 3

PostPosted: Sun Mar 23, 2003 7:32 am    Post subject: Reply with quote

Might I suggest?

Code:

select a1.salary1, count(*)
  from
   (select e1.salary as salary1,
           e2.salary as salary2   
      from employee e1,
           employee e2
     group by e1.salary, e2.salary) A1
 where a1.salary1 <= a1.salary2
 group by a1.salary1
 order by a1.salary1 desc;


as a start? It produces something like:

Code:

EMP_SALARY           RANKING
============================
52750.00                 1
46500.00                   2
41250.00                 3
40175.00                 4
38250.00                 5
36170.00                 6
32250.00                 7
29840.00                 8
29750.00                 9
29250.00                10
28760.00                11
28420.00                12
27740.00                13
27380.00                14
26250.00                15
26150.00                16
25370.00                17
25280.00                18
24680.00                19
23840.00                20
23800.00                21
22250.00                22
22180.00                23
21340.00                24
20450.00                25
19950.00                26
19180.00                27
18270.00                28
17750.00                29
17250.00                30
15900.00                31
15340.00                32


Edited - different statement which handles duplicates better
Back to top
View user's profile Send private message
rama krishna reddy
Beginner


Joined: 18 Sep 2006
Posts: 31
Topics: 13
Location: Hyderabad

PostPosted: Thu Apr 19, 2007 12:54 pm    Post subject: MAX(SAL) Reply with quote

Is there any other way to select the second or third max value in a column..apart from the below query.
select max(sal) from table where sal < (select max(sal) from table)


Hi Madhuri

We can select first max or third max whar ever u want by using below correlated query

select a.emplno ,a.salary from employee a where 0=(select count(*) from employee b where a.salary < b.salary)

In the above case it will give maximum salary ,if u want second max u need to give 1 inspite of 0 ,if u want third max u need to give 2 inspite of 0

eg:

i/p Employee table date is

500
400
200
300
100

query will sort the input table like below


In this case

a b

100 100
200 200
300 300
400 400
500 500


if u consider salary one in this case count will beocome 4 science

100 is less than 200,300,400,500

in case of 500 COUNT will be zero

I hope it will clear u r doubt

Regards
Rama krishna reddy
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12366
Topics: 75
Location: San Jose

PostPosted: Thu Apr 19, 2007 3:31 pm    Post subject: Reply with quote

rama krishna reddy,

Wrong thread. I guess you intended to post in this topic

http://www.mvsforums.com/helpboards/viewtopic.php?t=8224

Post the solution over there and I will delete it in this topic

Thanks

Kolusu
_________________
Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort

www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group