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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:48:47+00:00 2026-06-10T14:48:47+00:00

I have a table with a following schema: id, field, time 3, ‘test0’, 2012-08-29

  • 0

I have a table with a following schema:

id, field, time
3, 'test0', 2012-08-29
3, 'test1', 2012-08-30
3, 'test2', 2012-08-31
2, 'test0', 2012-08-19
2, 'test1', 2012-08-20
2, 'test2', 2012-08-26
...

I need for every id in the list find it’s last and previous to last value.
For example if the ids = [2,3] the result should return

3, 'test1', 2012-08-30
3, 'test2', 2012-08-31 
2, 'test1', 2012-08-20
2, 'test2', 2012-08-26

If i will need just last value, I would use

SELECT *
FROM table
WHERE id IN (2, 3)
GROUP BY id

Any ideas how I can achieve this?

  • 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-10T14:48:49+00:00Added an answer on June 10, 2026 at 2:48 pm

    If your times for each id number are unique, this will work.

    SELECT d.id, d.field,d.time
      FROM d d
      JOIN (
        SELECT max(d.time)m, d.id
          FROM d
          JOIN (
             SELECT max(time) m, id
               FROM d
           GROUP BY id
          )e ON d.id=e.id AND e.m>d.time
          GROUP BY d.id
       )e ON d.time >= e.m AND d.id = e.id
    

    Here’s how this works. This nested query gets the latest time for each id.

             SELECT max(time) m, id
               FROM d
           GROUP BY id
    

    Then, in turn this gets nested into this query, which gives you the second-to-latest time for each id (the latest time of the subset of rows that exclude the very latest time).

        SELECT max(d.time)m, d.id
          FROM d
          JOIN (
             SELECT max(time) m, id
               FROM d
           GROUP BY id
          )e ON d.id=e.id AND e.m > d.time
    

    Finally, the full query (shown first above) gets all the rows with times greater than or equal to the second latest time.

    If your times aren’t unique — that is, you have some rows where the identical ID and time appear, you may get more than two rows for each ID. But you’ll always get the two latest times.

    If a particular ID only has one row, you won’t get it.

    Only you know if those limitations are acceptable.

    Go fiddle! http://sqlfiddle.com/#!2/82719/5/0

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

Sidebar

Related Questions

I have the following schema: Database: test. Table: per_login_user, Field: username (PK), password Database:
I have a MySQL table with the following schema: +--------------+-------------+------+-----+---------+----------------+ | Field | Type
I have the following schema. Table votes +------------------+--------------+------+-----+---------------------+----------------+ | Field | Type | Null
I have a bunch of coordinates in my table grid with the following schema
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have no control over database schema and have the following (simplified) table structure:
I have the following schema (generated from an existing table with the schema module
I have a table name Discount that has the following schema: PK DiscountID int
I have a cvs file which schema is, every field is surrounded with ,
Consider the following scenario. I have a table (a stupid_table ) in a schema

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.