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.
| Author |
Topic |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-07-07 : 02:21:23
|
| dear friendsI have the table with employee Date of Joining details Which may contains some repeated Employee ID, because there is a possibility for employee will be transfered to the other department, and i have another table with the details of Attendence, i want to update the Department code corresponsing to the date in attendance table.Please find my sample tables below Table : EmpJoiningEmpId DOJ DepartmentCode1001 12-05-2010 Dep0011004 13-05-2010 Dep0201001 25-05-2010 Dep1001005 01-06-2010 Dep034 Table : Attendance (Before Exectute Query)EmpId AttendaceDate DepartmentCode1001 01-05-2010 NULL1001 02-05-2010 NULL1001 03-05-2010 NULL1001 04-05-2010 NULL1001 05-05-2010 NULL1001 06-05-2010 NULL1001 07-05-2010 NULL1001 08-05-2010 NULL1001 09-05-2010 NULL1001 10-05-2010 NULL1001 11-05-2010 NULL1001 12-05-2010 NULL1001 13-05-2010 NULL1001 14-05-2010 NULL1001 15-05-2010 NULL1001 16-05-2010 NULL1001 17-05-2010 NULL1001 18-05-2010 NULL1001 19-05-2010 NULL1001 20-05-2010 NULL1001 21-05-2010 NULL1001 22-05-2010 NULL1001 23-05-2010 NULL1001 24-05-2010 NULL1001 25-05-2010 NULL1001 26-05-2010 NULL1001 27-05-2010 NULL1001 28-05-2010 NULL1001 29-05-2010 NULL1001 30-05-2010 NULL1001 31-05-2010 NULLTable : Attendance (After Execute the Output should be like below)EmpId AttendaceDate DepartmentCode1001 01-05-2010 NULL1001 02-05-2010 NULL1001 03-05-2010 NULL1001 04-05-2010 NULL1001 05-05-2010 NULL1001 06-05-2010 NULL1001 07-05-2010 NULL1001 08-05-2010 NULL1001 09-05-2010 NULL1001 10-05-2010 NULL1001 11-05-2010 NULL1001 12-05-2010 Dep0011001 13-05-2010 Dep0011001 14-05-2010 Dep0011001 15-05-2010 Dep0011001 16-05-2010 Dep0011001 17-05-2010 Dep0011001 18-05-2010 Dep0011001 19-05-2010 Dep0011001 20-05-2010 Dep0011001 21-05-2010 Dep0011001 22-05-2010 Dep0011001 23-05-2010 Dep0011001 24-05-2010 Dep0011001 25-05-2010 Dep1001001 26-05-2010 Dep1001001 27-05-2010 Dep1001001 28-05-2010 Dep1001001 29-05-2010 Dep1001001 30-05-2010 Dep1001001 31-05-2010 Dep100For the sample i have used the EmpId 1001, But in the attendace table all other employee id will also available, and the values also should be updated on corresponding Employee Id also..Note : I want to execute a query and the attendance table need to be updated like the sample which i provided.Please help me to get the expected output, let me know if you have any questions |
|
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2010-07-07 : 06:58:46
|
| Use the script for update ,update Attendance set Attendance.DepartmentCode=EmpJoining.DepartmentCodefrom Attendance inner join EmpJoining on EmpJoining.EmpId=Attendance.EmpIdwhere EmpJoining.DOJ<=Attendance.AttendaceDateAND (CASE WHEN NOT(SELECT MIN(DOJ) FROM EmpJoining AS Emp WHERE Emp.DepartmentCode <> EmpJoining.DepartmentCode AND Emp.EmpId = EmpJoining.EmpId AND DOJ>=EmpJoining.DOJ)IS NULL THEN Attendance.AttendaceDate ELSE 1 END)<= (CASE WHEN NOT(SELECT MIN(DOJ) FROM EmpJoining AS Emp WHERE Emp.DepartmentCode <> EmpJoining.DepartmentCode AND Emp.EmpId = EmpJoining.EmpId AND DOJ>=EmpJoining.DOJ)IS NULL THEN DATEADD(D,-1,(SELECT MIN(DOJ) FROM EmpJoining AS Emp WHERE Emp.DepartmentCode <> EmpJoining.DepartmentCode AND Emp.EmpId = EmpJoining.EmpId AND DOJ>=EmpJoining.DOJ)) ELSE 1 END)SQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-07-07 : 07:16:33
|
Much simpler...UPDATE aSET a.DepartmentCode = ( SELECT TOP(1) e.DepartmentCode FROM dbo.EmpJoining AS e WHERE e.EmpId = a.EmpId AND e.DOJ <= a.AttendanceDate ORDER BY e.DOJ DESC )FROM dbo.Attendance AS a N 56°04'39.26"E 12°55'05.63" |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-07-07 : 07:49:44
|
| Thank you so much peso and sql programmers.Really much helpful. Thanks again |
 |
|
|
|
|
|