Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Return only employees with contracts with two or more departments

Posted on 2014-02-11
4
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

618 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