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
 

 

to get count of rows in another table for a key in 1 table

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

New User


Joined: 29 Dec 2005
Posts: 10

PostPosted: Mon Feb 09, 2009 11:59 am    Post subject: to get count of rows in another table for a key in 1 table
Reply with quote

I have 2 tables, DEPT & EMPLOYEE.

DEPT:
Dept Name
Dept No
Dept Loc

EMPLOYEE:
Employee Name
Employee Address
Salary
Dept No


I need an SQL to display Dept Name, Dept Loc, Dept No along with count of employees in each Dept.
Can anyone provide me with the query.
Back to top
View user's profile Send private message

dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Mon Feb 09, 2009 2:35 pm    Post subject:
Reply with quote

Quote:

Can anyone provide me with the query

better than that, we can help you build your own query. what do you have so far?
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Mon Feb 09, 2009 3:29 pm    Post subject:
Reply with quote

Please try the following query.

Code:
SELECT A.DEPTNAME,A.DEPTNO,A.DEPTLOC,B.CNT FROM
DEPT A, (SELECT DEPTNO, COUNT(*) as CNT from EMPLOYEE
GROUP BY DEPTNO) AS B
WHERE A.DEPTNO=B.DEPTNO
Back to top
View user's profile Send private message
naveengec

New User


Joined: 24 Mar 2007
Posts: 35
Location: pune

PostPosted: Mon Feb 09, 2009 3:35 pm    Post subject: Reply to: to get count of rows in another table for a key in
Reply with quote

Please try it...

select dept_name, dept_loc, dept_No, (select count(B.DEPT_NO) FROM EMPLOYEE B WHERE A.DEPT_NO=B.DEPT_NO) AS COUNT_EMP
FROM DEPT A
Back to top
View user's profile Send private message
i_at_forum

New User


Joined: 29 Dec 2005
Posts: 10

PostPosted: Tue Feb 10, 2009 6:36 pm    Post subject:
Reply with quote

thank you...
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 DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm
No new posts Row-Numbers of distinct rows? Auryn DB2 1 Thu Oct 20, 2016 4:38 pm
No new posts abend sort based on count records in ... anatol DFSORT/ICETOOL 5 Mon Oct 17, 2016 10:10 pm


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