SQL: Common table expression to update a segmented query