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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:19:17+00:00 2026-05-19T04:19:17+00:00

I have the following situation: In a table I have 4 columns: CommentsTBL: ID

  • 0

I have the following situation:

In a table I have 4 columns:

CommentsTBL:
ID int
Data datetime
Hour Varchar
Comment Varchar

The Hour column is of varchar type, don’t ask me why….

Given a specific date and hour from the table, I need to find the previous and the next record. If I will find one of them (previous or next) it will be simple to get the other one using the same logic.

As an example:

ID  Data        Hour    Comment
1.  2011-01-12  17:00   SomeComments1
2.  2011-01-14  10:00   SomeComments3
3.  2011-01-14  11:00   SomeComments4
4.  2011-01-13  14:00   SomeComments2

The previous date for ID 4 would be ID 1 and the next one will be ID 2.

I came up with a solution for this, but I want to see if any of you have something better, maybe using just one SELECT. This is for the previous one:

SELECT 
       Data,
       MAX(Hour) Hour
FROM 
       CommentsTBL
WHERE 
       Data in (SELECT 
                      MAX(Data) 
                FROM 
                      CommentsTBL
                WHERE 
                      Data < MyDate or (Data = MyDate and Hour < MyHour)
               )
GROUP BY
         Data

Thanks.

  • 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-19T04:19:18+00:00Added an answer on May 19, 2026 at 4:19 am

    Why not:

    select *
      from ( SELECT data, hour from commentsTbl
              where data < mydate
                 or ( data = mydate and hour < myhour )
              order by data desc, hour desc
              limit 1 
           )
     union all
    select *
      from ( SELECT data, hour from commentsTbl
              where data > mydate
                 or ( data = mydate and hour > myhour )
              order by data asc, hour asc
              limit 1
           )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following situation, String a=<em>crawler</em> <em> Yeahhhhh </em></a></h3><table; System.out.println(a.indexOf(</em>)); It returns the 11
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with
We've encountered the following situation in our database. We have table 'A' and table
I have following Situation, Server A sends some data (HTML form) to server B,
I have the following situation. Im getting data from an external system in the
I have the following situation I have a couple of table rows within a
i have a database table prop_amenities which have following columns here is the create
i have an situation where i have a following table. create table Bulk_Data (
I have situation where I am mapping table's columns to the primary key of
I am solving in these days following situation: In my DB table I have

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.