sql >> Database >  >> RDS >> Sqlserver

Dubbele rijen hernoemen

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

Als u een update-statement wilt, kunt u vrijwel dezelfde structuur gebruiken:

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. #1064 - Je hebt een fout in je SQL-syntaxis; controleer de handleiding die overeenkomt met uw MySQL-serverversie

  2. Introductie van de Easysoft Oracle® Driver in uw SOA-omgeving

  3. Dynamische Pivot nodig met Row_Number()

  4. Inner Join Three Tables