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

Query to find multiple rows


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sasanka
Warnings : 1

New User


Joined: 18 Jan 2008
Posts: 34
Location: India

PostPosted: Mon Jan 13, 2014 12:48 pm
Reply with quote

Hi,

We have an Employee Table (EMP_TAB) with columns EMP_NO, EMP_NAME, EMP_DEPT and data in the table are as below:

Code:
EMP_NO   EMP_NAME   EMP_DEPT

100         James          Sales
200         Thomas        Sales
300         Jacob           Marketing
400         Mark            Manufacturing
500         Eliza            Advertising
600         Julia            HR
700         Tom            Manufacturing
800         Sandy         Marketing

We need a query to find only those employee details where more than one employee is available in a dept.

Out put we need is as below:

(since Advertising and HR dept has only one employee, so we don't need these respective rows in my query result)

Code:
EMP_NO   EMP_NAME   EMP_DEPT

100          James         Sales
200          Thomas       Sales
300          Jacob          Marketing
400          Mark           Manufacturing
700          Tom            Manufacturing
800          Sandy         Marketing

Please help me with this query.

Thanks.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Mon Jan 13, 2014 7:37 pm
Reply with quote

Hi,

What you have tried so far?

See if below query helps

Code:
SELECT
A.EMP_NO,
A.EMP_NAME
FROM EMPLOYEE A
WHERE
A.EMP_DEPT
IN
(SELECT EMP_DEPT
  FROM EMPLOYEE
GROUP BY EMP_DEPT
HAVING COUNT(EMP_DEPT ) > 1)


Regards,
Chandan
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3052
Location: NYC,USA

PostPosted: Wed Jan 15, 2014 8:24 am
Reply with quote

Chandan , shouldn't that count be just = 1 instead of > 1 ?
Back to top
View user's profile Send private message
sureshpathi10

Active User


Joined: 03 May 2010
Posts: 154
Location: Kuala Lumpur

PostPosted: Wed Jan 15, 2014 9:09 am
Reply with quote

Hi Rohit,

Whatever coded by Chandan is the correct one as per the requirement.
Quote:
We need a query to find only those employee details where more than one employee is available in a dept.


But a small modification to improve performance (Since we would have very less department which has only one employee, so it would take small amount of time for sub-query to execute).

Code:

SELECT *
FROM EMPLOYEE A
WHERE A.EMP_DEPT
NOT IN
 (SELECT EMP_DEPT
  FROM EMPLOYEE
  GROUP BY EMP_DEPT
  HAVING COUNT(EMP_DEPT) = 1)
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3052
Location: NYC,USA

PostPosted: Wed Jan 15, 2014 9:11 am
Reply with quote

Thanks suresh...I took the requirement in the reverse way.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Wed Jan 15, 2014 11:50 am
Reply with quote

Agree with you Suresh..Thanks
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 INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts To get the count of rows for every 1 ... DB2 3
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts RC query -Time column CA Products 3
Search our Forums:

Back to Top