You can perform an UPDATE based on the results of your SELECT query by utilizing a self-join with conditions that match your requirements. In this case, since you want to update records only if the start_DTS is after the end_DTS, you’ll need to construct an UPDATE query that joins the table to itself and applies the necessary date conditions.
Here’s an example of how you could structure the query:
UPDATE tableA AS A
JOIN tableA AS B
ON A.name = B.name
AND A.criteria = 1
AND B.criteria = 2
AND B.start_DTS > A.end_DTS
SET A.linked_id = B.id;
Explanation:
Self-Join on the Same Table: Here, we’re joining tableA to itself, giving each side an alias (A and B) to differentiate the sets of data.
Applying the Filtering Criteria: We let A denote the rows where criteria = 1 (the end date-times) and B where criteria = 2 (the start date-times).
Setting the Date-Time Condition: The condition B.start_DTS > A.end_DTS only includes records in which B's start date-time is after A's end date-time.
Linking the Records: The query updates A.linked_id (presuming this column is intended to store a reference to the associated id of B) with B.id where the conditions are met.
This will link tableA records by updating linked_id with the id of matching entries, where appropriate. It’s always a good idea to back up your data before running the UPDATE to prevent accidental changes in case of unexpected matches.