Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Question

Author  Topic 

EugeneLim11
Posting Yak Master

167 Posts

Posted - 2008-03-24 : 07:56:31
I have a table employeeDepartmentHistory with the following fields

1. EmployeeID, GUID (FK to Employee Table)
2. DepartmentID,GUID (FK to departmentTable)
3. StartDate, DateTime (Record the date which the employee start working in the department)
4. EndDate, DateTime (Record the date which employee left the department)

My question is how do I check for duplicate records in StartDate for each Employee? e.g.

EmployeeID Department StartDate EndDate
John Sales 13 Mar 2008 13 Mar 2008
John Accounts 13 Mar 2008 null
Ember Sales 14 Mar 2008 null
Apple Sales 15 Mar 2008 null

I want to list all records for an employee who has identical start date (e.g. John) but I could not come out with the correct SQL statement to get what I need. Any help is very much appreciated.

It should display
EmployeeID Department StartDate EndDate
John Sales 13 Mar 2008 13 Mar 2008
John Accounts 13 Mar 2008 null

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-03-24 : 08:06:03
select distinct e1.EmpId,e1.DeptId,E1.StartDate from emp_detail e1
inner join emp_detail e2
on e1.empid=e2.empid
and e1.deptid<>e2.deptid
and e1.startdate=e2.startdate
Go to Top of Page

EugeneLim11
Posting Yak Master

167 Posts

Posted - 2008-03-24 : 08:18:39
Thank you. Funny how I could not get it, but seems so simple to others.
Go to Top of Page

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-03-24 : 08:36:46
Please note that the above query will not return any rows if the duplication is for the same department
Go to Top of Page
   

- Advertisement -