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 7890127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:16:26+00:00 2026-06-03T06:16:26+00:00

I have a list of items that I need to re-sequence with no gaps.

  • 0

I have a list of items that I need to re-sequence with no gaps. It starts out as:

ID   Sequence
123    1
125    2
155    3
158    4
165    6
170    9

I need to end up with (Note that Sequence 6 changes to 5 and Sequence 9 changes to 6)

ID   Sequence
123    1
125    2
155    3
158    4
165    5
170    6

I have tried using this update statement

UPDATE  tblA
SET     tblA.Sequence = temp.Sequence
FROM    ( SELECT    id ,
                    ROW_NUMBER() OVER ( ORDER BY Sequence, ID ) AS Sequence
          FROM      dbo.tblA
        ) AS temp

but I just end up with …

ID   Sequence
123    1
125    1
155    1
158    6
165    6
170    6

Pulling the select statement out of the update produces the correct results.
Changing it to something like

 UPDATE tblA
 SET    tblA.Sequence = temp.NewSequence
 FROM   ( SELECT    id ,
                    ROW_NUMBER() OVER ( PARTITION BY id ORDER BY Sequence, id ) 
                                                               AS NewSequence
          FROM      dbo.tblA
        ) AS temp

Produces the results

ID   Sequence
123    1
125    1
155    1
158    1
165    1
170    1

What am I doing wrong?

  • 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-03T06:16:27+00:00Added an answer on June 3, 2026 at 6:16 am

    You need to associate your re-numbered set with the IDs from your original set, otherwise you’re updating the original ID with a sequence number for some other ID from your temp set:

    UPDATE a
    SET a.Sequence = temp.Sequence
    FROM    
        tblA a JOIN
        (
            SELECT id, ROW_NUMBER() OVER (ORDER BY Sequence, ID) AS Sequence
            FROM dbo.tblA
        ) AS temp ON temp.ID = a.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of items that I need to update on different intervals.
I have items in a list in a SharePoint 2003 site that I need
I have a list of work items that need to be processed in order.
I want to have a list of items that need to be processed in
I have a list of items that I need to bubble sort. The bubble
I have a list of items that I want to be laid out horizontally.
I have a list of items that has numeric values and I need to
I have a list of items that I need to validate. The list can
I have list of items that need to be processed and printed to the
I have a list of items that I need to record its position in

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.