i want to update new field in 'ActualDate' field in 'tbl_Task' table . fetch data from 'tbl_TaskHistory' 'CreatedDate' field .Task ID is commen in two table. Same task id exist multiple times in task history. i want to fetch first data from task History table based on created date asc.
Loading

Tahir AnsariRank #366 · 6.8K reputationPosted Aug 1, 2023, 6:45 AM EDT
UPDATE tbl_Task SET ActualDate = th.CreatedDate FROM tbl_Task t INNER JOIN ( SELECT TaskID, MIN(CreatedDate) AS CreatedDate FROM tbl_TaskHistory GROUP BY TaskID ) th ON t.TaskID = th.TaskIDTarunRank #6,374 · 268 reputationPosted Aug 1, 2023, 3:59 AM EDT
to restrict multiple ids use pk and use first() function
Amit MohantyRank #112 · 20K reputationPosted Aug 1, 2023, 4:23 AM EDT
UPDATE t SET t.ActualDate = h.CreatedDate FROM tbl_Task t JOIN ( SELECT TaskID, CreatedDate, ROW_NUMBER() OVER (PARTITION BY TaskID ORDER BY CreatedDate ASC) as rn FROM tbl_TaskHistory ) h ON t.TaskID = h.TaskID AND h.rn = 1;Manikandan MurugesanRank #300 · 8.4K reputationPosted Aug 1, 2023, 1:25 PM EDT
UPDATE t SET t.ActualDate = th.CreatedDate FROM tbl_Task t INNER JOIN ( SELECT TaskID, CreatedDate, ROW_NUMBER() OVER (PARTITION BY TaskID ORDER BY CreatedDate ASC) AS rn FROM tbl_TaskHistory ) th ON t.TaskID = th.TaskID AND th.rn = 1;To fetch the earliest record from the
tbl_TaskHistorytable based on theCreatedDatefor eachTaskIDand update the correspondingActualDatein thetbl_Tasktable, you can use a SQL query with theROW_NUMBER()function to rank the records byCreatedDatein ascending order and then perform an update.Cr BhargaviRank #420 · 5.9K reputationPosted Aug 5, 2023, 8:21 AM EDT
UPDATE tbl_Task SET ActualDate = th.CreatedDate FROM tbl_Task AS t INNER JOIN ( SELECT TaskID, MIN(CreatedDate) AS CreatedDate FROM tbl_TaskHistory GROUP BY TaskID ) AS th ON t.TaskID = th.TaskID WHERE t.ActualDate IS NULL OR t.ActualDate > th.CreatedDate;