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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:35:52+00:00 2026-06-02T07:35:52+00:00

Suppose I have the following table: id name base index 0 A 2 0

  • 0

Suppose I have the following table:

id  name  base   index
0   A     2      0
1   B     2      2
2   C     2      4
3   D     2      6
4   E     2      8
5   F     2      10

So, index = base * i, where i is the position of that row in a sequence.

From time to time, some rows are deleted, for instance, if I delete rows named C and D:

id  name  base   index
0   A     2      0
1   B     2      2
4   E     2      8
5   F     2      10

New rows are always added after the last, so the next row would be MAX(index)+base=12 in this case, but the gap left between values in the index column due to the deleted rows would be a problem after some time. If instead of inserting last I insert it in the first available gap, the problem won’t happen.

So, I doubt any queries to find the first available gap would be as efficient as the MAX(index), but what would the most efficient solution be? Maybe it’s good enough.

In case it’s not clear, I need to find the first row ‘a’ such that the row with the closest upper index value is more than a.index + a.base.

This is meant for an application using an ORM for any SQL databases, so it has to be strictly standard SQL.

edit

This is a simplification of the real table and the real problem, and I’m looking for a solution using the base and index columns only. Solutions involving adding new columns or indexing in other table are not practical for my application.

edit 2

It seems the base column is making it more complicated, but that isn’t essential. The problem can be reduced to a table like:

id  name  index
0   A     0
1   B     1
4   E     4
5   F     5

Where I need to find the first row ‘a’ such that the row with the lowest index that’s higher than a.index + x. In this case x = 1.

Enumerating without ordering first or leveraging on the id aren’t reliable solutions, since those can change. For instance, a solution has to work if the rows are like this too:

id  name  index
0   A     0
23  F     5
45  E     4
90  B     1
  • 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-02T07:35:54+00:00Added an answer on June 2, 2026 at 7:35 am

    It’s not clear to me what your question means if there are multiple values of “base” in the table. Does the “row with the closest upper index value” have to have the same value of “base”, for example?

    In any case, this might be a start, if you are using a SQL platform that implements the function LEAD(). You may have to rephrase TOP in the appropriate dialect. Replace 999999999 with any value greater than the largest possible value of index+base.

    with LeadAdded as (
      select 
        lead(index,1,999999999) over (order by index) as nxt,
        *
      from yourTable
    )
      select top (1) *
      from LeadAdded
      where nxt > index + base;
      order by index
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose that I have the following table: employee (id, name, surname, salary); The
Suppose I have a table with the following rows id Name Price Supplier 1
Suppose I have the following table: PARAMETER - NAME varchar2(10) - TABLE_NAME varchar2(50) -
Suppose I have the following table: PERSON : ID INT NAME STRING LASTNAME STRING
Suppose I have the following entities: @Entity @Table(name = A) public class A implements
Suppose I have following table in my database: Tbl_Persons: Id Country Name 1 Australia
Suppose I have the following student table: id | name | datecreated ------------------------ 1
Suppose i have the following table book ----- id name genre check_out_date location_id Can
suppose I have the following source table (called S): name gender code Bob 0
Suppose I have the following table house: House: id name cityID Where cityID refers

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.