Solved

Return only employees with contracts with two or more departments

Posted on 2014-02-11
4
206 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 31

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

759 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

24 Experts available now in Live!

Get 1:1 Help Now