Need to change only the first row in a table that has duplicate records

Suppose I have a database table with members of a club. Each member is assigned a unique id. In the club table I have a column that is used to mark a row if it is a duplicate of other rows. This column is named "IsDup". so if you look at the table below all duplicate rows are marked with a  column value of "1" in the IsDup column.
Below is the table, and I have attached a sample table having scripted it out with insert statements in sql server.

I need to update the data in the table, so that the very first duplicate is marked with a value of "8" in the IsDup column, and the other duplicates for a club member retain their IsDup value of "1". (Note if a club member does not have duplicates, then his or her IsDup column is not modified). Can someone help me create the update statement for this?

Below is an example of my ClubMembers table before and after the update.

Before update
 ID      Fname  Lname     IsDup
95X    Narnia   Jones,      1
95X    Narnia   Jones,      1
95X    Narnia   Jones,      1
89X    Tamra   Gleeson   NULL
90E     Amy     Sloan        1
90E     Amy     Sloan

After (Update)

 ID      Fname  Lname     IsDup
95X    Narnia   Jones,      8
95X    Narnia   Jones,      1
95X    Narnia   Jones,      1
89X    Tamra   Gleeson   NULL
90E     Amy     Sloan        8
90E     Amy     Sloan        1
TheClub.txt
LVL 2
brgdotnetcontractorAsked:
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.

Vikas GargAssociate Principal EngineerCommented:
Hello,

You can use the Row_number function

SELECT *,CASE WHEN RN =1 THEN 8 ELSE ISDUP END ISDUP
FROM
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY ID, FNAME,LNAME,ISDUP ORDER BY ID) RN FROM TABLE
) TBL

Open in new window

0

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Try this:
UPDATE CM
SET CM.IsDup = 8
FROM (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RN 
	FROM ClubMembers
	WHERE IsDup IS NOT NULL) CM
WHERE CM.RN=1

Open in new window

0
brgdotnetcontractorAuthor Commented:
Thanks Guys. Vitors solution worked 10%. Vikas, yours was close, but the people with only one record were having their IsDup flag set to 8, and I needed that value to not change. Plus I like Vitors solution because it did not involve a temp table. Thank you guys so much.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Only 10%? What are the 90% that are missing?
0
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

From novice to tech pro — start learning today.