I have below tables with employee and his colleagues data,i want to update bucketid in Employee table, if there is matching Colleagues with the other employee.
here in this example,
Employee = 101 is matching with Employee = 103 Colleague(i.e. c1) so both should have same bucketid = 1(i.e. min of both bucketids)
and Employee = 102 is matching with Employee = 103 Colleague(i.e. c3) so both should have same bucketid, but here it should be get updated with 1 as employee=102 bucketid has just changed to 1. we have transitive law dependent data in this example.
(i.e. a=b and b=c then a=c)
Employee table:
EmployeeID EmployeeName BucketID
101 williams 1
102 williams 2
103 williams 3
104 williams 4
Employee_Colleague table:
EmployeeID Colleague
101 c1
101 c2
102 c3
102 c4
103 c1
103 c3
104 c7
I tried using this update query,
update a2
set BucketID = a1.BucketID
from Employee a1
inner join Emp_Colleagues c1 on a1.EmployeeID=c1.EmployeeID
inner join Employee a2 on a1.EmployeeName=a2.EmployeeName
inner join Emp_Colleagues c2 on a2.EmployeeID=c2.EmployeeID
where c1.Colleague=c2.Colleague and a1.BucketID <> a2.BucketID
it is returning below output.
EmployeeID EmployeeName BucketID
101 williams 1
102 williams 1
103 williams 3
104 williams 4
But i want output as below in Employee table.
EmployeeID EmployeeName BucketID
101 williams 1
102 williams 1
103 williams 1
104 williams 4