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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:28:30+00:00 2026-05-26T20:28:30+00:00

I have two tables that I designed this way with a possible reshuffling of

  • 0

I have two tables that I designed this way with a possible reshuffling of elements in mind:

1. [dbo.test_db_002] with columns:
[id] = INT NOT NULL IDENTITY(1,1) PRIMARY KEY
[name] = NVARCHAR(255)

and

2. [dbo.test_db_003] with columns:
[ord] = INT
[itmid] = INT NOT NULL PRIMARY KEY

[itmid] column has a constraint linking it to [dbo.test_db_002].[id] like so:

ALTER TABLE [dbo.test_db_003] 
ADD CONSTRAINT fk1 FOREIGN KEY ([itmid]) 
REFERENCES [dbo.test_db_002]([id]) 
ON DELETE CASCADE ON UPDATE CASCADE;

Say, [dbo.test_db_002] table has the following data:

[id] [name] 
3    John
5    Mary
8    Michael
10   Steve
13   Jack
20   Pete

and [dbo.test_db_003] has the following ordering data:

[ord] [itmid]
1      5
4      8
5      13
8      3
10     10
13     20

So when I retrieve names from the database I use the following SQL:

SELECT [name]
FROM   [dbo.test_db_002] t1
LEFT JOIN [dbo.test_db_003] t2 ON t1.[id]=t2.[itmid]
ORDER BY t2.[ord] ASC

It produces the list of names (ordered by the [dbo.test_db_003].[ord] column):

Mary
Michael
Jack
John
Steve
Pete

What I am looking for is an option to move each of the names up and down the list. For instance, if I want to move “John” one position up, what do I do?

So far I came up with this partial SQL:

WITH cte AS
(
    SELECT [id], [ord], ROW_NUMBER() OVER (ORDER BY t2.[ord] ASC) AS rowNum
    FROM [dbo.test_db_002] t1
    LEFT JOIN [dbo.test_db_003] t2 ON t1.[id] = t2.[itmid]
)

That will select the following:

rowNum  [id]  [ord]
1        1     5
2        4     8
3        5     13
4        8     3
5        10    10
6        13    20

So I understand that I need to shift values in [ord] column up by one starting from the index 3 (since “John” index is 4) and then somehow make “John”‘s [ord] to be set to 5, but how do you do that?

  • 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-26T20:28:31+00:00Added an answer on May 26, 2026 at 8:28 pm

    I prepared a complete demo for you how this can work on data.stackexchange.com.
    The solution is tailored to your comment:

    the move up or down can be only a single step – in other words, one
    cannot move 2 or more positions

    In the example I make John trade ordinal positions with Jack above him:

    WITH x AS (
      SELECT t2.itmid, t2.ord
      FROM   dbo.test_db_002 t1
      LEFT   JOIN dbo.test_db_003 t2 ON (t1.id = t2.itmid)
      WHERE  t1.name = 'John'  -- must be unique, or query by id ...
      )
      , y AS (
      SELECT TOP 1
             t.itmid, t.ord
      FROM   dbo.test_db_003 t, x
      WHERE  t.ord < x.ord     -- smaller ord = "above"
      ORDER  BY t.ord DESC
      )
    UPDATE dbo.test_db_003 SET ord = z.ord
    FROM (
       SELECT x.itmid, y.ord FROM x,y
       UNION ALL
       SELECT y.itmid, x.ord FROM x,y
       ) z
    WHERE  dbo.test_db_003.itmid = z.itmid   
    

    ###Major points:

    1. Use two CTE to structure the query:
    2. Get John’s id & ordinal position
    3. Get the same for the person above him
    4. Prepare two rows where these two switch ordinal numbers with the help of UNION ALL
    5. Use these two rows in a now simple UPDATE
    • The ordinal position ord must allow passing duplicates for this to work.
    • If there is nobody ‘above’, the query will silently do nothing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two tables that look like this: TH TH TH TH
I have a DB table with two int columns that are not nullable, but
I have two tables that are joined together. A has many B Normally you
I have two tables that should be joined together by a foreign key relationship,
I have two tables that are considered a single entity in my domain model.
I have two tables that I am joining with the following query... select *
I have two tables that are related via a mapping table: keywords titles I
Suppose I have two tables that are linked (one has a foreign key to
If i have two tables that have nothing in common I want to do
Ok, I have two tables that I am joining and doing a select on

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.