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

  • SEARCH
  • Home
  • 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 9212783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:36:14+00:00 2026-06-18T01:36:14+00:00

I need help to get the solution for this condition. I have a table

  • 0

I need help to get the solution for this condition. I have a table containing records, there is a field sku, in this record i have sku’s appearing multiple times. Table structure is like this rid|id|sku|name

if any sku is available on table multiple times the record looks like this

rid  id  sku     name
---  --  ------  --------------

1    3   rs-123  test product

2    3   rs-123  test product

3    4   rs-125  test product 2

4    4   rs-125  test product 2

5    4   rs-125  test product 2

6    6   rs-126  test product 3

What I need is to update the table with duplicate records keeping first record unchanged (N-1). So for example I need to run SQL statement which update only duplicate records, so if first record is updated it will look like this

rid  id  sku     name
---  --  ------  --------------
1    3   rs-123  test product
2    3   updated  updated 

I was trying to achieve something with this SQL statement but it is not working

WITH duplicates AS (
  SELECT
ROW_NUMBER() OVER (PARTITION BY id ORDER BY rid) AS duplicate_id,
*
FROM
test
)

UPDATE
  duplicates
SET
  sku = updated
WHERE
  duplicate_id > 1

Any advise would be highly appreciated.

  • 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-06-18T01:36:16+00:00Added an answer on June 18, 2026 at 1:36 am

    Perhaps you may give it a try with a variable:

    SQLFIDDLE DEMO

    set @sku:='';
    
    select a.rid, a.id, 
    @sku:= (case when @sku<>a.sku
            then a.sku else 'updated'
            end) as skus, @sku:=a.sku, a.name
    from skus a
    ;
    
    | RID | ID |    SKUS | @SKU:=A.SKU |           NAME |
    -----------------------------------------------------
    |   1 |  3 |  rs-123 |      rs-123 |   test product |
    |   2 |  3 | updated |      rs-123 |   test product |
    |   3 |  4 |  rs-125 |      rs-125 | test product 2 |
    |   4 |  4 | updated |      rs-125 | test product 2 |
    |   5 |  4 | updated |      rs-125 | test product 2 |
    |   6 |  6 |  rs-126 |      rs-126 | test product 3 |
    

    For updating:

    SQLFIDDLE DEMO for UPDATING

    set @sku:='';
    
    UPDATE
      skus a
      join 
      (select a.rid, a.id, 
    @sku:= (case when @sku<>a.sku
            then a.sku else 'updated'
            end) as skus, @sku:=a.sku, a.name
    from skus a) b
      on a.rid = b.rid
    SET
      a.sku = 'up_again'
    WHERE
      b.skus = 'updated'
    ;
    
    | RID | ID |      SKU |           NAME |
    ----------------------------------------
    |   1 |  3 |   rs-123 |   test product |
    |   2 |  3 | up_again |   test product |
    |   3 |  4 |   rs-125 | test product 2 |
    |   4 |  4 | up_again | test product 2 |
    |   5 |  4 | up_again | test product 2 |
    |   6 |  6 |   rs-126 | test product 3 |
    

    Here is another using Joins:

    http://sqlfiddle.com/#!2/ddf47/2

    update skus a
    join skus b
    on a.rid = b.rid +1
    set a.sku = 'updated'
    where a.sku = b.sku
    ;
    
    
    | RID | ID |     SKU |           NAME |
    ---------------------------------------
    |   1 |  3 |  rs-123 |   test product |
    |   2 |  3 | updated |   test product |
    |   3 |  4 |  rs-125 | test product 2 |
    |   4 |  4 | updated | test product 2 |
    |   5 |  4 | updated | test product 2 |
    |   6 |  6 |  rs-126 | test product 3 |
    

    To update both sku and name:

    http://sqlfiddle.com/#!2/97f4f/1

    update skus a
    join skus b
    on a.rid = b.rid +1
    set a.sku = 'updated', a.name = 'new_name'
    where a.sku = b.sku
    ;
    
    | RID | ID |     SKU |           NAME |
    ---------------------------------------
    |   1 |  3 |  rs-123 |   test product |
    |   2 |  3 | updated |       new_name |
    |   3 |  4 |  rs-125 | test product 2 |
    |   4 |  4 | updated |       new_name |
    |   5 |  4 | updated |       new_name |
    |   6 |  6 |  rs-126 | test product 3 |
    

    LOOKING AT OP’S QUESTION having used ROW_NUMBER() IN SQL SERVER

    http://sqlfiddle.com/#!3/355c4/2

    update a 
    set a.sku = 'updated', a.name = 'new_name'
    from skus a
    join (Select rid, id, row_number() over (
      partition by id order by rid asc) rank
    from skus) b
    on a.rid = b.rid
    where b.rank > 1
    ;
    
    | RID | ID |     SKU |           NAME |
    ---------------------------------------
    |   1 |  3 |  rs-123 |   test product |
    |   2 |  3 | updated |       new_name |
    |   3 |  4 |  rs-125 | test product 2 |
    |   4 |  4 | updated |       new_name |
    |   5 |  4 | updated |       new_name |
    |   6 |  6 |  rs-126 | test product 3 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help to get the solution for this condition. I have a table
I need some help to get this right, please. I have created an iPad
I need help in this matter: We have a template of Excel file in
I need help finding a viable solution to convert bbcode to html, this is
I'm having hard time to get the right solution for this, so I need
I need help to get a response when I click on an Item from
Need your help to get the max of CAP_PRICE based on certain criteria in
I need help regarding JOIN tables to get category name & author name from
i got some problem and need help .. my plan : 1. get ip
I need a little help here: I get a file from an HTML upload

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.