I have a two tables i need to copy from table 2 to table 1. I want the table 1. column departure to be updated on the matching in table 2.
Condition
The table 2 have multiple items for the table 1,( table 1 have many relation with table 2). So in that case i need to sort it by RotationOrder and takes first row.
Table 1
Id Code Departure
479 JJ1256 NULL
480 SR1255 NULL
481 PFOBLEM NULL
482 SO1301 NULL
483 TS1302 NULL
484 YB1305 NULL
485 CU1303 NULL
Table 2
Id Departure RotationOrder CanLoad
479 NULL 1 1
480 NULL 1 2 1
481 NULL 1 3 1
482 NULL 1 4
482 NULL 3
482 NULL 2
482 NULL 4
483 2013-01-21 1 1
483 NULL 3
483 NULL 4
483 NULL 6
What i have tried
UPDATE table1 set Departure = (select top 1 table2.Departure from table2
INNER JOIN table1 on table1.Id = table2.Id where CanLoad =1 order by
RotationOrder )
FROM TABLE1 INNER JOIN TABLE2
ON TABLE1.Id = TABLE2.Id
Problem This query copy first null value from the table2 and paste it on table1. which is incorrect.