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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:36:36+00:00 2026-06-12T00:36:36+00:00

I have a table which is not sorted by any of column. Is there

  • 0

I have a table which is not sorted by any of column. Is there any way to select next/previous record if I know only Id of current? (I’m using mssql)

Id     Label     Date
---------------------
1      label1    2011-01-10
7      label2    2011-01-15 -- how to get previous?
5      label3    2011-01-12 -- I know id of this record
10     label10   2011-01-25 -- how to get next?
12     label8    2011-01-13
2      label5    2011-01-29

Thanks in advance!

  • 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-12T00:36:37+00:00Added an answer on June 12, 2026 at 12:36 am

    If you really want the previous from the list you enclosed, here is a way.

    declare @t table(Id int, Label varchar(10), Date date, s int identity(1,1))
    insert @t (id, label, date) 
    values(1,'label1','2011-01-10'),(7,'label2','2011-01-15'),
    (5,'label3','2011-01-12'),(10,'label10','2011-01-25'),
    (12,'label8','2011-01-13'),(2,'label5','2011-01-29')
    
    --select the data with a self join
    
    select t1.id as previous_id, t2.id, t2.Label, t2.Date, t3.id, t3.id as next_id
    from @t t1
    right join
    @t t2 on t1.s + 1 = t2.s
    left join
    @t t3  on t2.s = t3.s - 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in mysql which is not normalized and I need to
I have six tables, which unfortunately does not have any primary/foreign key-relations encoded. I've
I have table in which I am inserting rows for employee but next time
I have created a grid in Magento and there is a column which is
I have table which contains double values stored in mysql database ...I need to
i have a table which has many fields but i want to get count
I have a table which i want to store all the outputs into one
I have one table which contains events and dates, and another which contains the
I have a table which stores the data related to posts in this format
I have a table which contains TV Guide data. In a simplified form, the

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.