x

sql query based on many to many relationships

I have 3 tables Employee,Project and Assignment. An employee can be assigned to multiple projects and one project can have multiple employees. Assignment table keeps many to many relationship between employees and projects. I want a sql query in sql server that returns a list of employees that have ever worked on more than one project at the same time.

Structure of tables

Table Employee

EmpId,

EmpName

Table Project

ProjId,

ProjName, ProjStartDate, ProjEndDate

Table Assignment

AssignmentId, ProjId, EmpId, AssStartDate, AssEndDate

more ▼

asked Dec 21, 2012 at 07:28 AM in Default

avatar image

aroradhruv73
0 1 2 4

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first
 select ass.Eid,count(pid) NumOfProjects from Assignment ass
 inner join 
 (select eid,MIN(AssStartDate) as StartDate,MAX(AssEndDate) as EndDate from Assignment
 group by eid) as abc 
 on ass.Eid = abc.Eid
 where ass.AssStartDate between abc.StartDate and abc.EndDate
 group by ass.Eid having COUNT(ass.pid) > 1
more ▼

answered Dec 21, 2012 at 08:31 AM

avatar image

aroradhruv73
0 1 2 4

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x449

asked: Dec 21, 2012 at 07:28 AM

Seen: 1165 times

Last Updated: Dec 23, 2012 at 02:55 AM

Copyright 2018 Redgate Software. Privacy Policy