IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Need a query for which has max records in the table


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
k_kirru
Currently Banned

New User


Joined: 14 Sep 2005
Posts: 16

PostPosted: Tue Mar 11, 2008 11:49 am
Reply with quote

Hi All,

I need a query to the below req..

I have an employee table with say 11 employees. I want to extract the dept number with max no of employees.

Below is the Table:

EMP_NO DEPT_NO
01 D01
02 D02
03 D03
04 D01
05 D03
06 D01
07 D02
08 D01
09 D02
10 D03
11 D01

The above table is just an assumption.
The query should return the DEPT_NO with max no of employees. Ie., D01 5(no of employees in this dept)..

Thanks in advance...

Waiting for your reply.
Regards,
Kiran
Back to top
View user's profile Send private message
Prajesh_v_p

Active User


Joined: 24 May 2006
Posts: 133
Location: India

PostPosted: Tue Mar 11, 2008 12:27 pm
Reply with quote

Try out this query..I did nt test this one

select deptno, count(*) as emptotal from emp_table
group by deptno
order by emptotal
fetch first row only

hope this helps..

Prajesh
Back to top
View user's profile Send private message
sainathvinod

New User


Joined: 01 Apr 2008
Posts: 11
Location: Chennai

PostPosted: Wed Apr 02, 2008 2:05 pm
Reply with quote

The above quer will fetch only one record even if there are more than one dept having the maximum number of employees. Please try the below query which will fetch all the depts(incase there are more than 1) having the maximum number of employees:-

SELECT DEPT_NO
,COUNT(*)
FROM DEPT
GROUP BY DEPT_NO
HAVING COUNT(*) =
(SELECT MAX(TEMP.A) FROM
(SELECT DEPT_NO
,COUNT(*) A
FROM DEPT
GROUP BY DEPT_NO) TEMP)
WITH UR;
_________________
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
No new posts RC query -Time column CA Products 3
No new posts Multiple table unload using INZUTILB DB2 2
Search our Forums:

Back to Top