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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:07:50+00:00 2026-05-27T14:07:50+00:00

I have a table including the columns (int id pkey, int group, double sortOrder)

  • 0

I have a table including the columns (int id pkey, int group, double sortOrder) where sortOrder implements a user-specified sort order within one group. On some modifications, I need to re-number the sort order of all items in the group. SortOrder values in different groups are independen of each other (i.e. they are only ever compared within one group)

SELECT id, sortOrder FROM tbl WHERE group=X ORDER BY SortOrder

Gives me all elements in that group, in the current order, in whch I would have to assign sequential values (1, 2, 3, …) to SortOrder.

Q: Is there any reasonable way – preferrably portable to other SQL implementations – to do that without updating every row individually?


More info: I am using doubles, because that allows to trivially assign a new sort order without modifying other items (MIN-1 for before, MIN+1 for insert at end, and (A+B)/2 for inserting between A and B.) – this is limited of course by double resolution (~52 inserts in the worst case). I’m not sure yet if this is worth the additional checking for overflow, but I’d have the same problem with any other data type anyway.

The only other idea I came up with was simulating infinite resolution with strings and custom COLLATE and ADD/SUB/AVG functions. However, this seems immensely non-portable.

  • 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-27T14:07:51+00:00Added an answer on May 27, 2026 at 2:07 pm

    I don’t believe that SQLite has a trivial way of doing this.

    What you may be better looking at (if this really is a concern) is sticking with integer sorting and implementing an algorithm that maintains the sequence.

    If, for example, all changes to sorting involve moving an item up/down one place, you just need to swap the sort orders of both values…

    UPDATE table
    SET sort_order = CASE WHEN sort_order = 3 THEN 4 ELSE 3 END
    WHERE sort_order IN (3,4)
    

    Or if you can move an item to any position, use something like…

    UPDATE table
    SET sort_order = CASE WHEN sort_order = @old THEN @new ELSE sort_order + 1
    WHERE sort_order >= @new AND sort_order <= @old
    

    I’m aware that you said that you don’t want to update other items each time, but this has actually proven to be pretty efficient in most cases for me in the past.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called scheduler_sched which has several columns, including a column called
Good day, In SQL Server 2005, I have a table numerous columns, including a
I have created a new table including a column note. The default is varchar(255)
I have a table that represents the parameters of a contract - including their
I have a table that has 8 million records, with many fields, including lat/long
I have table with 3 columns A B C. I want to select *
I have created the following trigger for insert in the deleted user table the
Using SQL2k5, I have a staging table that contains columns that will populate numerous
I have a MySQL table Page with 2 columns: PageID and OrderByMethod. I also
I have a table with some denormalized precalculated columns that are maintained by a

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.