Skip to content
Advertisement

Oracle – update the same count of duplicates between two tables

I have two tables UPD_REF and UPD_TO

There is no key and we can have duplicates in both tables UPD_TO or UPD_REF.

I need to update the same count of rows on UPD_TO corresponding to UPD_REF.

If I do a simple update or merge like this:

I have all the matching values updated:

But I need to get that:

Any clues ?

Advertisement

Answer

You can update your rows by rowid, but first you need to identify that single row in each group by some ordering criteria in ROW_NUMBER(). The code is:

If your table has more columns, then it may be good solution. If this is the exact table you have and you need to have a count of rows with the same ID and NAME, then it is better not to insert new rows, but update and count rows (of course, altering table before with new column for counter):

I cannot imagine another useful reason to have such two-column table with duplicates, that’s why I’ve asked for clarification of the purpose.

UPD: New version, that updates the same row count per REF, that UPD_REF has, with fiddle containing duplicates in UPD_REF.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement