Solved

Return only employees with contracts with two or more departments

Posted on 2014-02-11
4
214 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:
Scott Pletcher 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 88
Query Optimization 14 42
MS SQL tables, .net application and ETL connundrum 3 54
VBScript Write Column Headers 3 36
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 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