Solved

Return only employees with contracts with two or more departments

Posted on 2014-02-11
4
212 Views
Last Modified: 2014-02-16
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

Open in new window

- 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

Open in new window

- without returning Karen, since she has contract with only one dep, and Peter who has only one contract with one dep
0
Comment
Question by:fmsol
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39852667
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

Open in new window

0
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 250 total points
ID: 39852679
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)
                                  

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39853475
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
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 39853853
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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now