Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4026222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:56:36+00:00 2026-05-20T10:56:36+00:00

Here’s a simplified example of my problem. I have a table where there’s a

  • 0

Here’s a simplified example of my problem. I have a table where there’s a “Name” column with duplicate entries:

ID    Name
---   ----
 1    AAA
 2    AAA
 3    AAA
 4    BBB
 5    CCC
 6    CCC
 7    DDD
 8    DDD
 9    DDD
10    DDD

Doing a GROUP BY like SELECT Name, COUNT(*) AS [Count] FROM Table GROUP BY Name results in this:

Name  Count
----  -----
AAA   3
BBB   1
CCC   2
DDD   4

I’m only concerned about the duplicates, so I’ll add a HAVING clause, SELECT Name, COUNT(*) AS [Count] FROM Table GROUP BY Name HAVING COUNT(*) > 1:

Name  Count
----  -----
AAA   3
CCC   2
DDD   4

Trivial so far, but now things get tricky: I need a query to get me all the duplicate records, but with a nice incrementing indicator added to the Name column. The result should look something like this:

ID    Name
---   --------
 1    AAA
 2    AAA (2)
 3    AAA (3)
 5    CCC 
 6    CCC (2)
 7    DDD 
 8    DDD (2)
 9    DDD (3)
10    DDD (4)

Note row 4 with “BBB” is excluded, and the first duplicate keeps the original Name.

Using an EXISTS statement gives me all the records I need, but how do I go about creating the new Name value?

SELECT * FROM Table AS T1 
WHERE EXISTS (
    SELECT Name, COUNT(*) AS [Count] 
    FROM Table 
    GROUP BY Name 
    HAVING (COUNT(*) > 1) AND (Name = T1.Name))
ORDER BY Name

I need to create an UPDATE statement that will fix all the duplicates, i.e. change the Name as per this pattern.

Update:
Figured it out now. It was the PARTITION BY clause I was missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:56:36+00:00Added an answer on May 20, 2026 at 10:56 am
    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
    

    If you want an update statement you can use pretty much the same structure:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I ran into recently. I have attributes strings of the form
here is my problem: I have a jList and a popup menu. When I
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here's a coding problem for those that like this kind of thing. Let's see
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here we go again, the old argument still arises... Would we better have a
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here's a basic regex technique that I've never managed to remember. Let's say I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.