sql query

Hi,

I have a duplicate in item's name, so I want to add number 1,2,3,.. for any duplicate
on new field
Result:,Thanks
f1         f2
coca   coca1
coca   coca2
samprgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

almanderCommented:
SELECT Row_Number() OVER(PARTITION BY F1 ORDER BY F1),
  F1,
  F2
FROM YOUR_TABLE_NAME

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samprgAuthor Commented:
Nice, but I need to update F1 with number
Thanks

Result:
f1         f2
coca   coca1
coca   coca2
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

strickddCommented:
SELECT F1,
  F2 + Row_Number() OVER(PARTITION BY F1 ORDER BY F1)
FROM YOUR_TABLE_NAME
samprgAuthor Commented:
Good, I wanna
1-Update statemnet
2- Just for duplicate
strickddCommented:
UPDATE myTable
Set
   Column1 = Value1
WHERE
    F1 IN (SELECT F1,
  F2 + Row_Number() OVER(PARTITION BY F1 ORDER BY F1)
FROM YOUR_TABLE_NAME)
Brendt HessSenior DBACommented:
Question:  Do you only want to update the rows that have duplicates?  If so, the query is more complex.  Also, you will need to include a unique identifier to specifically target the rows needed.


UPDATE MyItems
SET ItemName = mi.ItemName + CAST(rnum as varchar(5))
FROM  MyItems mi
INNER JOIN (
    SELECT ItemName,
        ItemID,
        Row_Number() OVER(PARTITION BY ItemName) rnum
    FROM MyItems mif
    INNER JOIN (
        SELECT ItemName, Count(*) ct
        FROM MyItems mi2
        GROUP BY ItemName
        HAVING COUNT(*) > 1
        ) multi
        ON multi.itemName = mif.ItemName
    ) Filt
    ON mi.ItemID = Filt.ItemID
Brendt HessSenior DBACommented:
Ah, for the ability to edit.  You don't actually need to return ItemName from the Filt query...
samprgAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.