• Hi Chris,

    Here is my attempt doing this with a rCTE

    ;WITH retained AS (

    SELECT t.retained, dropped

    FROM testing t

    WHERE NOT EXISTS (SELECT 1 FROM testing i WHERE i.dropped = t.retained)

    UNION ALL

    SELECT b.retained, t1.dropped

    FROM testing AS t1

    INNER JOIN retained as b

    ON t1.retained = b.dropped)

    UPDATE t

    SET t.retained = uv.retained

    FROM testing t

    INNER JOIN retained uv

    ON t.dropped = uv.dropped

    This works with the sample data but something isn't quite right with this. Tried it with a sample of 160000 records, it took 17 seconds.

    When I increased the sample record set to 200000, the query just keeps going. It's scaling up badly or there's some dodgy data that the above logic just can't handle efficiently.

    This is what the estimated execution plan is telling me:

    Tried adding a couple of indexes on retained and dropped columns but that made no difference to the estimated execution plan.

    Arrrggghhh! :w00t:

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn