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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:55:35+00:00 2026-05-27T11:55:35+00:00

I have table t: id, timestamp There are multiple id values, and multiple rows

  • 0

I have table t:

id, timestamp

There are multiple id values, and multiple rows might share a given id.

I want to select the most recent row for each id, before x date, only if id is not found after x date, and id is also not found in table y.

I can select all before x date, in this example :date=5 :

SELECT * FROM t WHERE timestamp < :date

I attempted to get most recent id only, not getting most recent–but returning 1 row per id:

SELECT * FROM t WHERE timestamp < :date GROUP BY id ORDER BY timestamp DESC

I’m concerned GROUP BY will slow things with lots of data.

Here is some sample db data:

CREATE TABLE IF NOT EXISTS `t` (
  `id` int(2) NOT NULL,
  `timestamp` int(2) NOT NULL
) 
INSERT INTO `t` (`id`, `timestamp`) VALUES
(1, 1),
(1, 4),
(2, 3),
(2, 1),
(2, 6),
(3, 4),
(3, 2);

CREATE TABLE IF NOT EXISTS `y` (
  `id` int(2) NOT NULL,
  `timestamp` int(2) NOT NULL
) 
INSERT INTO `y` (`id`, `timestamp`) VALUES
(3, 1);

Looking to return row (1,4) only…

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-27T11:55:36+00:00Added an answer on May 27, 2026 at 11:55 am

    You need to select with a MAX to get the latest time (rather than sorting) do a LEFT JOIN to compare data in the other table, and a HAVING as an argument to GROUP BY to only select the appropriate data.

    SELECT t.id, MAX(t.timestamp) AS latest_timestamp
    FROM t
    LEFT JOIN y on t.id = y.id
    WHERE y.id IS NULL
    GROUP BY t.id
    HAVING latest_timestamp <= :date
    

    When you do a GROUP BY you can select with aggregate functions. Here MAX returns the maximum value for that column in all the rows in the group (since you are grouping by id, this will return the maximum timestamp for each id). But you only want to select elements that don’t have a timestamp after :date — that’s where HAVING comes in (HAVING is essentially a WHERE for GROUP BY aggregates). Finally, you don’t want to select elements that are in table y. So you LEFT JOIN table y in, and only select rows where the corresponding row in table y doesn’t exist (i.e. that id doesn’t exist in table y); you do this using a regular WHERE.

    UPDATE: To make this efficient, all you have to do is add indexes to the appropriate columns. In this case, you would want to add indexes for t.id, t.timestamp, and y.id. See dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html.

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

Sidebar

Related Questions

I have a table where each row has a timestamp. Is there a query
I have an SQL table like this : sales(product,timestamp) I want to display a
Is there a reason why you would want to have multiple KEYs in a
I want to have one table with two TIMESTAMP columns. One column to keep
I have a table lead and there is a field called added_on (datatype timestamp)
I have a table where each row needs a timestamp marking the time it
I have a table in SQLite: CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)
I have a table in a MSSQL database that looks like this: Timestamp (datetime)
I have a table with a float called 'cost' and timestamp called'created_at'. I would
I have a table with 3 fields: ID (not unique, not primary key) timestamp

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.