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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:39:47+00:00 2026-05-16T10:39:47+00:00

I have a table I’d like to sort with a priority column. This column

  • 0

I have a table I’d like to sort with a “priority” column. This column needs to be reordered when the priority of a record is changed or records are removed. Think of it as an array. The values will be modified in a UI so I want them to remain whole numbers and represent the true position within the larger recordset. The priority column won’t have NULLs.

id       priority
1        2
2        1
3        4
4        3

Now say I change the priority of id 4 to 2 or I insert or delete a row how do I get all priorities to reshuffle so there are no gaps or duplicates and the highest possible priority is always the number of rows?

The table has a “date_modified” field which is accurate to the second and updated on insert/update so if needed it is possible to know which record was modified last (to break a tie when 2 records have the same priority)

  • 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-16T10:39:47+00:00Added an answer on May 16, 2026 at 10:39 am

    Assuming you have 8.4 you can use window functions.

    UPDATE test_priority 
    SET priority = sub.new_priority
    FROM (
        SELECT user_id, id, priority, rank() OVER (ORDER BY priority, date_modified) new_priority
        FROM test_priority
        WHERE user_id = $1
    ) sub 
    WHERE test_priority.user_id = sub.user_id 
      AND test_priority.id = sub.id
      AND test_priority.priority <> sub.new_priority
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with 300 000 records (MyISAM). I get record from this table
I have table and this table contain result column with some entries. I just
I have table called Direccion other called Cliente each one defined like this public
I have table field defined as like this approved_at DATETIME YEAR TO SECOND NOT
I have table in an MS Access database that looks like this: ID Symbol
I have table X, and each record in X has many records in Y.
I have table (call it my_table ) that can be simplified like this: NAME,
I have table like this: +------+-------+ | user | data | +------+-------+ | 1
I have table structure like this: ID cond 1 5 1 2 1 6
I have table data like this id id1 name 1 1 test1 1 1

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.