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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:27:54+00:00 2026-06-04T06:27:54+00:00

Say that I have some SELECT statement: SELECT id, name FROM people ORDER BY

  • 0

Say that I have some SELECT statement:

SELECT id, name FROM people
   ORDER BY name ASC;

I have a few million rows in the people table and the ORDER BY clause can be much more complex than what I have shown here (possibly operating on a dozen columns).

I retrieve only a small subset of the rows (say rows 1..11) in order to display them in the UI. Now, I would like to solve following problems:

  1. Find the number of a row with a given id.
  2. Display the 5 items before and the 5 items after a row with a given id.

Problem 2 is easy to solve once I have solved problem 1, as I can then use something like this if I know that the item I was looking for has row number 1000 in the sorted result set (this is the Firebird SQL dialect):

SELECT id, name FROM people
   ORDER BY name ASC
   ROWS 995 TO 1005;

I also know that I can find the rank of a row by counting all of the rows which come before the one I am looking for, but this can lead to very long WHERE clauses with tons of OR and AND in the condition. And I have to do this repeatedly. With my test data, this takes hundreds of milliseconds, even when using properly indexed columns, which is way too slow.

Is there some means of achieving this by using some SQL:2003 features (such as row_number supported in Firebird 3.0)? I am by no way an SQL guru and I need some pointers here. Could I create a cached view where the result would include a rank/dense rank/row index?

  • 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-04T06:27:55+00:00Added an answer on June 4, 2026 at 6:27 am

    Firebird appears to support window functions (called analytic functions in Oracle). So you can do the following:

    To find the “row” number of a a row with a given id:

    select id, row_number() over (partition by NULL order by name, id)
    from t
    where id = <id>
    

    This assumes the id’s are unique.

    To solve the second problem:

    select t.*
    from (select id, row_number() over (partition by NULL order by name, id) as rownum
          from t
         ) t join 
         (select id, row_number() over (partition by NULL order by name, id) as rownum
          from t
          where id = <id>
         ) tid
         on t.rownum between tid.rownum - 5 and tid.rownum + 5
    

    I might suggest something else, though, if you can modify the table structure. Most databases offer the ability to add an auto-increment column when a row is inserted. If your records are never deleted, this can server as your counter, simplifying your queries.

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

Sidebar

Related Questions

We have a statement., that is inserting some rows in a temporary table (say
I have read from some article that say's Apple doesn't approve the application which
In Python, say I have some class, Circle, that inherits from Shape. Shape needs
In PHP, say that you have some code like this: $infrastructure = mt_rand(0,100); if
Let say that I have a website with some information that could be access
Let's say that I have class , that uses some functionality of dict .
Say I have some 10 categories that I need to reference in a web
Let's say I have some Control that has been disabled. It contains a bunch
Let's say I have some grid that looks like this _ _ _ _
Let's say I have some code (using CherryPy) that looks like this: import cherrypy

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.