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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:09:15+00:00 2026-05-30T23:09:15+00:00

I have a table such as (simplified to the extreme to make it clearer)

  • 0

I have a table such as (simplified to the extreme to make it clearer)

create table mytable (
  id integer not null,
  owner text not null,
  order_field_1 integer not null,
  order_field_2 integer not null
)

I’m trying to get the next and previous elements’ ids every time I get a row from the database, to allow navigation. The rows are not ordered by id, but by ORDER BY order_field_1 DESC, order_field_2 DESC.

When getting the last entries for an owner, I have no problem to find what I want using a window and lead/lag

SELECT
  id,
  owner,
  lag(id) over w AS previous_id,
  lead(id) over w AS next_id
FROM
  mytable
WHERE
  owner = 'someuser'
WINDOW w AS (
  ORDER BY order_field_1 DESC, 
  order_field_2 DESC
)
ORDER BY 
  order_field_1 DESC, 
  order_field_2 DESC
LIMIT
  5

This is written from memory but that’s the gist of it, and it works perfectly.

My problem is when I want to get a specific row, using owner AND id, yet I still want to find the previous and next ids, I can not use a window function anymore since only one row is returned by the where, and my current solution of doing a subquery to get both navigation id is not very good performance wise

For exemple (I only put previous id since it’s the same for next)

SELECT
  m1.id,
  m1.owner,
  (
    SELECT 
      m2.id 
    FROM 
      mytable m2 
    WHERE 
      m2.owner = m1.owner 
      AND m2.id != m1.id 
      AND (
        m2.order_field_1 < m1.order_field_1 
        OR (
          m2.order_field_1 = m1.order_field_1 
          AND m2.order_field_2 <= m1.order_field_2
        ) 
      ORDER BY 
        m2.order_field_1 DESC, 
        m2.order_field_2 DESC
      LIMIT
        1
  ) AS previous_id
FROM
  mytable m1
WHERE
  owner = 'someuser'
  AND id = 12345

So I’m selecting my row, then selection the first row from the same user, with a different id, that is either with a lower order_field_1 or the same but a lower order_field_2.

This is not really efficient and I am getting poor performances, and I’m wondering if anyone has any idea on how I could improve it ?

Exemple dataset:

id |    owner | order_field_1 | order_field_2
 1 | someuser |             4 |             2
 2 | someuser |             2 |             8
 3 | someuser |             4 |             3
 4 | someuser |             3 |             2
 5 | someuser |             4 |             6
 6 | someuser |             4 |             5

Ordered:

id |    owner | order_field_1 | order_field_2
 5 | someuser |             4 |             6
 6 | someuser |             4 |             5
 3 | someuser |             4 |             3
 1 | someuser |             4 |             2
 4 | someuser |             3 |             2
 2 | someuser |             2 |             8

If I select owner = ‘someuser’ and id = 3, previous_id should be 1, next_id should be 6.

If I select owner = ‘someuser’ and id = 1, previous_id should be 4, next_id should be 3.

Thanks in advance for any help

  • 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-30T23:09:16+00:00Added an answer on May 30, 2026 at 11:09 pm

    How about finding the lag and lead values before applying the WHERE clause?

    WITH T as (
      SELECT
        id,
        owner,
        lag(id) over w AS previous_id,
        lead(id) over w AS next_id
      FROM
        mytable
      WINDOW w AS (
        ORDER BY order_field_1 DESC, 
        order_field_2 DESC
      )
    )
      SELECT * FROM T
      WHERE
        owner = 'someuser' AND id = 3
      ORDER BY 
        order_field_1 DESC, 
        order_field_2 DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some table with such schema (simplified): CREATE TABLE folders( id INT(11) UNSIGNED
I have a table such as: PK_ID LOCATION_ID LOCATION_TEXT 1 21 NULL 2 44
I have a query as such (simplified): var q = from t in _entities.Table
I have a table such as: table a +------+-------+ | v1 | v2 |
If I have a table such as: info_name | info_value name | Susan desc
I have table with Names such as JS Engineering Services$Web User Setup Header and
Lets say I have a table with columns such as: ID Name City State
i have a table with the columns such id, tid, companyid, ttype etc.. the
I have a table of artist names such as Lady Gaga, Jason Mraz, Death
I have Employee table and an Entity class for it, My task is such

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.