Issue with a USER_DIM table in a database that has references to other tables in that database like USER_ACTIVITY_FACT via USER_ID.
A problem arouse when the original design of the table did not use a proper unique identifier which actually comes from a different database's EXTERNAL_ID.
I can figure how to select the extra rows or delete them but I want to update the rows in the other tables to point to the original USER_ID and then delete the extra rows in the USER_DIM table
SELECT ACTIVITY_FACT.USER_ID FROM USER_ACTIVITY_FACT WHERE USER_ACTIVITY_FACT.USER_ID IN ( select USER_ID FROM USER_DIM WHERE EXTERNAL_ID IN (SELECT ud2.EXTERNAL_ID FROM USER_DIM as ud2 where USER_ID > ud2.USER_ID)); update these to be the smallest USER_ID
And then execute the delete statement on the USER_DIM;
DELETE FROM USER_DIM WHERE EXTERNAL_ID IN (SELECT ud2.EXTERNAL_ID FROM USER_DIM as ud2 where USER_ID > ud2.USER_ID); after which ALTER the table to have Unique index on the EXTERNAL_ID column.
This query might update per row rather than a more preferred update multiple rows at once for the offending-extra USER_ID.
Thanks in advance for your help!
UPDATE To clarify the objective:
USER_ACTIVITY_FACT ------------- USER_ID 2 3 4 5 6 USER_DIM -------------- USER_ID EXTERNAL_ID 2 23 3 23 4 24 5 24 6 26 ..result should look like
USER_ACTIVITY_FACT ------------- USER_ID 2 2 4 4 6 USER_DIM -------------- USER_ID EXTERNAL_ID 2 23 4 24 6 26 hope this helps