This is my situation.
TABLE1:
DOCUMENT_ID,
GUID
TABLE2:
DOCUMENT_ID,
FILE
The tables are joined by DOCUMENT_ID, meaning that TABLE2 can have one or many rows with the same DOCUMENT_ID.
My problem is that TABLE2 values for whole bunch of DOCUMENT_ID have same FILE values.
I need a SQL query that will get me all GUID and count how many rows in TABLE2 for this DOCUMENT_ID have EXACTLY THE SAME FILE value (so that I can copy the GUID to Excel).
Then I need to UPDATE TABLE2‘s FILE columns for these cases.
For instance if DOCUMENT_ID has three rows in TABLE2 with same FILE value, I need to update two of them by adding a postfix like FILEVALUE-1, FILEVALUE-2 and so on.
Hope I make sense.
To all experts thank you in advance.
To get duplicates you might employ oldfashioned group by:
To directly update duplicates, you might use CTE:
Note that I’ve added ID as a placeholder for primary key. You need a way to identify a record to be updated.
There is live test @ Sql Fiddle.