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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:26:18+00:00 2026-05-13T15:26:18+00:00

I want to be able to get the next and previous row using SQLite.

  • 0

I want to be able to get the next and previous row using SQLite.

id  statusid  date
168 1   2010-01-28 16:42:27.167
164 1   2010-01-28 08:52:07.207
163 1   2010-01-28 08:51:20.813
161 1   2010-01-28 07:10:35.373
160 1   2010-01-27 16:09:32.550
 46 2   2010-01-30 17:13:45.750
145 2   2010-01-30 17:13:42.607
142 2   2010-01-30 16:11:58.020
140 2   2010-01-30 15:45:00.543

For example:

Given id 46 I would like to return ids 160 (the previous one) and 145 (the next one)

Given id 160 I would like to return ids 161 (the previous one) and 46 (the next one)
etc…

Be aware that the data is ordered by statusId then dateCreated DESC and HAS to work using SQLite.

select * from @t order by statusId, dateCreated desc

Test data created in sql server…

set nocount on; set dateformat ymd;
declare @t table(id int, statusId int, dateCreated datetime)
insert into @t
select 168,1,'2010-01-28 16:42:27.167' union
select 164,1,'2010-01-28 08:52:07.207' union
select 163,1,'2010-01-28 08:51:20.813' union
select 161,1,'2010-01-28 07:10:35.373' union
select 160,1,'2010-01-27 16:09:32.550' union
select  46,2,'2010-01-30 17:13:45.750' union
select 145,2,'2010-01-30 17:13:42.607' union
select 142,2,'2010-01-30 16:11:58.020' union
select 140,2,'2010-01-30 15:45:00.543'

Using SQL server 2005+ this would be fairly trivial!

EDIT:

Here’s the same test data script but for SQLite which is the focus of the question.

create table t (id int, statusId int, dateCreated datetime);
insert into t
select 168,1,'2010-01-28 16:42:27.167' union
select 164,1,'2010-01-28 08:52:07.207' union
select 163,1,'2010-01-28 08:51:20.813' union
select 161,1,'2010-01-28 07:10:35.373' union
select 160,1,'2010-01-27 16:09:32.550' union
select  46,2,'2010-01-30 17:13:45.750' union
select 145,2,'2010-01-30 17:13:42.607' union
select 142,2,'2010-01-30 16:11:58.020' union
select 140,2,'2010-01-30 15:45:00.543';

EDIT 2 Please note that the data is not a good example so I have change the id 146 to 46

  • 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-13T15:26:18+00:00Added an answer on May 13, 2026 at 3:26 pm

    This problem is a lot more complicated than it first appears. The two order by fields have to be handled separately and then combined with a union and grab the appropriate result. To get both previous and next, we need another union, so we end up with a union with sub-unions.

    This works with the supplied data. I tested many inputs and got the right previous/next outputs. When using, make sure you get ALL instances of 146 to replace.

    SELECT *
    FROM
    (
        SELECT  t1.*
        FROM    t t1,
                (
                    SELECT  *
                    FROM    t
                    WHERE   id = 146
                ) t2
        WHERE   t1.statusid = t2.statusid
          AND   t1.dateCreated >= t2.dateCreated
          AND   t1.id <> 146
    
        UNION
    
        SELECT  t1.*
        FROM    t t1,
                (
                    SELECT  *
                    FROM    t
                    WHERE   id = 146
                ) t2
        WHERE   t1.statusid < t2.statusid
    
    
        ORDER BY             
                t1.statusid DESC,
                t1.dateCreated 
    
        LIMIT 1
    )
    
    UNION
    
    SELECT *
    FROM 
    (
        SELECT  t1.*
        FROM    t t1,
                (
                    SELECT  *
                    FROM    t
                    WHERE   id = 146
                ) t2
        WHERE   t1.statusid = t2.statusid
          AND   t1.dateCreated <= t2.dateCreated
          AND   t1.id <> 146
    
        UNION
    
        SELECT  t1.*
        FROM    t t1,
                (
                    SELECT  *
                    FROM    t
                    WHERE   id = 146
                ) t2
        WHERE   t1.statusid > t2.statusid
    
    
        ORDER BY             
                t1.statusid,
                t1.dateCreated DESC
    
        LIMIT 1
    )
    
    ORDER BY             
            statusid,
            dateCreated DESC
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 404k
  • Answers 404k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use a Panel and set the default acceptbutton.… May 15, 2026 at 5:36 am
  • Editorial Team
    Editorial Team added an answer I have looked at F#, doing low level tutorials, so… May 15, 2026 at 5:36 am
  • Editorial Team
    Editorial Team added an answer tcpdump uses libpcap; there are docs describing the libpcap file… May 15, 2026 at 5:36 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.