Updating Duplicate data in a table to Make it Unique

  • I have a table with 6120 records

    2275 records = Unique LicenseNumber

    1941 records = duplicate x2

    11 records = x3

    5 records = x4

    ProductLicense_Idn (PK, int, not null)

    LicenseNumber (Varchar(255), not null)

    The LicenseNumber needs to be unique and I have to modify only the duplicates entities as such.

    so for example

    ProductLicense_Idn LicenseNumber

    1 SYD12N

    2 toronto

    3 toronto

    4 AER45SF

    5 AER45SF

    6 AER45SF

    ...becomes

    ProductLicense_Idn LicenseNumber

    1 SYD12N

    2 toronto - 1

    3 toronto - 2

    4 AER45SF - 1

    5 AER45SF - 2

    6 AER45SF - 3

    so that I have a table with 6120 unique records

    assistance much appreciated!

    Thanks

    S

  • schwizzla (5/5/2009)


    I have a table with 6120 records

    2275 records = Unique LicenseNumber

    1941 records = duplicate x2

    11 records = x3

    5 records = x4

    ProductLicense_Idn (PK, int, not null)

    LicenseNumber (Varchar(255), not null)

    The LicenseNumber needs to be unique and I have to modify only the duplicates entities as such.

    so for example

    ProductLicense_Idn LicenseNumber

    1 SYD12N

    2 toronto

    3 toronto

    4 AER45SF

    5 AER45SF

    6 AER45SF

    ...becomes

    ProductLicense_Idn LicenseNumber

    1 SYD12N

    2 toronto - 1

    3 toronto - 2

    4 AER45SF - 1

    5 AER45SF - 2

    6 AER45SF - 3

    so that I have a table with 6120 unique records

    assistance much appreciated!

    Thanks

    S

    perhaps like this:

    select license_id

    , LicenseNumber AS old_name

    , LicenseNumber + ' - ' + str(ROW_NUMBER() OVER(ORDER BY LicenseNumber asc)) AS 'new_name'

    from tab1

  • Hi Tobe_ha,

    that works great but I am ending up with the follwing

    ProductLicense_Idn LicenseNumber

    1 SYD12N - 1

    2 toronto - 2

    3 toronto - 3

    4 AER45SF - 5

    5 AER45SF - 5

    6 AER45SF - 6

    How can I restart the numbering?

    e.g

    ProductLicense_Idn LicenseNumber

    1 SYD12N - 1

    2 toronto - 1

    3 toronto - 2

    4 AER45SF - 1

    5 AER45SF - 2

    6 AER45SF - 3

    Thanks

    S

  • try this code, it should work fine, but you may need to change it slightly if you dont want the single unique rows to be numbered

    UPDATE [YourTable]

    SET [LicenseNumber] = LicenseNumber + '-' + CAST(TheOrder AS CHAR(4))

    FROM (SELECT ProductLicense_Idn, ROW_NUMBER() OVER (PARTITION BY LicenseNumber ORDER BY ProductLicense_Idn) AS TheOrder FROM YourTable)AS sub

    WHERE [YourTable].[ProductLicense_Idn] = sub.ProductLicense_Idn

  • works a treat 🙂

    thanks for your help...

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply