sql >> Database teknologi >  >> RDS >> Sqlserver

Omdøb dublerede rækker

With Dups As
    (
    Select Id, Name
        , Row_Number() Over ( Partition By Name Order By Id ) As Rnk
    From Table
    )
Select D.Id
    , D.Name + Case
                When D.Rnk > 1 Then ' (' + Cast(D.Rnk As varchar(10)) + ')'
                Else ''
                End As Name
From Dups As D

Hvis du vil have en opdateringserklæring, kan du bruge stort set den samme struktur:

With Dups As
    (
    Select Id, Name
        , Row_Number() Over ( Partition By Name Order By Id ) As Rnk
    From Table
    )
Update Table
Set Name = T.Name + Case
                    When D.Rnk > 1 Then ' (' + Cast(D.Rnk As varchar(10)) + ')'
                    Else ''
                    End
From Table As T
    Join Dups As D
        On D.Id = T.Id


  1. Sikker MySQL-adgangskode på delt hosting

  2. Sådan indstilles tidszone for Postgres 9.4 permanent

  3. bruge triggere til at sikre datakonsistens

  4. Java Opretter forbindelse til ekstern MySQL-database