• Status: Solved
• Priority: Medium
• Security: Public
• Views: 229

# Return only employees with contracts with two or more departments

I have three tables; employees (e), departments (d) and contracts (c)
Each employee can have one or more contracts with one or more departments

Example:
Karen: 2 contracts, both with dep A
John: 1 contract with dep A, 2 contracts with dep B
Lisa: 2 contracts with dep C, 2 contracts with dep D
Peter: 1 contract with dep A

``````SELECT e.empID, e.name, e.uid, d.depID, d.depname, d.locaton from
employees e
LEFT JOIN contracts c ON c.empID=e.empID
LEFT JOIN departments d ON d.depID=c.depID
``````
- returns all employees with all departments they have a contract with

But: I want only those who has contracts with two or more departments.
In the example above: John and Lisa. Karen and Peter has contracts with only one dep.

I guess it would be something like:
``````SELECT e.empID, e.name, e.uid, d.depID, d.depname, d.locaton, COUNT(DISTINCT something) from
employees e
LEFT JOIN contracts c ON c.empID=e.empID
LEFT JOIN departments d ON d.depID=c.depID
GROUP BY  something
HAVING the group by > 1
``````
- without returning Karen, since she has contract with only one dep, and Peter who has only one contract with one dep
0
fmsol
• 2
2 Solutions

You can do that by the below query, but you can get only the employee name and employee ID.

``````SELECT e.empID, e.name, e.uid
employees e
JOIN contracts c ON c.empID=e.empID
JOIN departments d ON d.depID=c.depID
GROUP BY  e.empID, e.name , e.uid
HAVING COUNT(1) > 1
``````
0

If you want all the details including the contract details then you can use the below query and if you are using SQL Server 2005 or above version.

``````;WITH C AS
(
SELECT e.empID, e.name, e.uid, d.depID, d.depname, d.locaton
,row_number() OVER(partition by e.empID, e.name, e.uid order by e.empID, e.name, e.uid) RN
from employees e
JOIN contracts c ON c.empID=e.empID
JOIN departments d ON d.depID=c.depID
)
SELECT * FROM C c0 WHERE EXISTS ( SELECT 1 FROM C c1 and c0.empid = c1.empid and c0.uid = c1.uid WHERE RN >= 2)

``````
0

Commented:
Can you describe the contracts and department tables?  I assume there would be attributes in the contracts table, such as contractid that could be used to get the counts, and attributes in either or both of the two tables, such as contractwithdepartment in the contracts table or contractsresponsiblefor in the departments table, that can tie them together. Perhaps some sample data for all three tables and the expected output would help.
0

Senior DBACommented:
SELECT e.empID, e.name, e.uid, d.depID, d.depname, d.locaton
FROM (
SELECT e.empID
FROM employees e
INNER JOIN contracts c ON c.empID=e.empID
GROUP BY e.empID
HAVING COUNT(DISTINCT c.depID) > 1
) AS e_multi_dept
INNER JOIN employees e ON e.empID=e_multi_dept.empID
LEFT JOIN contracts c ON c.empID=e.empID
LEFT JOIN departments d ON d.depID=c.depID
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.