Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

SQL Query with maximum number of employees and the count

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
SSR
Warnings : 1

New User


Joined: 26 Feb 2006
Posts: 38

PostPosted: Thu Sep 07, 2006 2:24 pm    Post subject: SQL Query with maximum number of employees and the count
Reply with quote

Hi,

My requirement is

I have a table with the below columns

EMP_NO, EMP_NAME,DEPT_NO,DEPT_NAME

I want Department with maximum number of employees and the count.

Regards
SSR
Back to top
View user's profile Send private message

mbr_raja

New User


Joined: 15 Nov 2004
Posts: 26
Location: Chennai, India

PostPosted: Thu Sep 07, 2006 5:32 pm    Post subject: Re: SQL Query with maximum number of employees and the count
Reply with quote

Check the query and you will get the desired result as you expected.

select max(DEPT_NO), count(*) from < Table Name > ;
Back to top
View user's profile Send private message
sihanature
Warnings : 1

New User


Joined: 01 Sep 2005
Posts: 33

PostPosted: Thu Sep 07, 2006 5:50 pm    Post subject:
Reply with quote

That query looks fine.
Execute and see for desired results.

Regards
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1452
Location: Chicago, IL

PostPosted: Thu Sep 07, 2006 6:54 pm    Post subject: Re: SQL Query with maximum number of employees and the count
Reply with quote

SSR...

Try this query...

Code:
SELECT COUNT(EMP_NO) AS EMPCOUNT, DEPT_NO   
FROM DB2ID.TABLENAME
GROUP BY DEPT_NO                         
ORDER BY EMPCOUNT DESC FETCH FIRST ROW ONLY;
Back to top
View user's profile Send private message
SSR
Warnings : 1

New User


Joined: 26 Feb 2006
Posts: 38

PostPosted: Fri Sep 08, 2006 9:46 am    Post subject: Re: SQL Query with maximum number of employees and the count
Reply with quote

Thanks a lot priyesh, its working fine.
Back to top
View user's profile Send private message
SSR
Warnings : 1

New User


Joined: 26 Feb 2006
Posts: 38

PostPosted: Fri Sep 08, 2006 12:20 pm    Post subject: Re: SQL Query with maximum number of employees and the count
Reply with quote

hi priyesh,

Is there any way by which we could used the max function for this as this would be a simple one, but i did various trials but no use.

Regards
Sundar
Back to top
View user's profile Send private message
Rameshs

New User


Joined: 15 Jun 2005
Posts: 53
Location: India, Chennai

PostPosted: Fri Sep 08, 2006 3:02 pm    Post subject:
Reply with quote

Code:

SELECT EMPCOUNT,DEPT_NO
FROM TABLE
(SELECT COUNT(EMP_NO) AS EMPCOUNT, DEPT_NO   
FROM DB2ID.TABLENAME
GROUP BY DEPT_NO                         
ORDER BY EMPCOUNT DESC FETCH FIRST ROW ONLY) AS TB
WHERE EMPCOUNT =MAX(EMPCOUNT);

Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts CICS Transaction attach count of supp... lind sh CICS 1 Wed Jun 21, 2017 1:33 pm
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts Check if any Detail records and extra... V S Amarendra Reddy SYNCSORT 19 Mon May 08, 2017 8:54 pm
No new posts SMTP the current generation number of... Jyothi Kulunde JCL & VSAM 4 Thu May 04, 2017 4:08 pm
This topic is locked: you cannot edit posts or make replies. Merge two files and update Trailer re... Yashashri JCL & VSAM 1 Thu May 04, 2017 12:54 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us