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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:17:01+00:00 2026-05-17T02:17:01+00:00

… where missing records are identical to the last recorded value, hence no record.

  • 0

… where “missing records” are identical to the last recorded value, hence no record.

This may be subjective, but I’m hoping there’s a standardised way of doing this.

So, let’s say I have a bunch of analytics in a MySQL table. There is some missing information, but as mentioned above, that’s because their previous value is the same as the current value.

table "table":

id    value      datetime
1     5          1285891200    // Today
1     4          1285804800    // Yesterday
2     18         1285804800    // Yesterday
2     16         1285771094    // The day before yesterday

As you can see, I don’t have a value for today for id 2.

If I wanted to pull the “most recent value” from this table (that is, 1’s “today”, and 2’s “yesterday”, how do I do that? I’ve achieved it by running the following query:

SELECT id, value FROM (SELECT * FROM table ORDER BY datetime DESC) as bleh GROUP BY id

Which utilizes a subquery to order the data first, and then I rely on “GROUP BY” to pick the first value (which, since it is ordered, is the most recent) from each id. However, I don’t know if shoving a subquery in there is the best way to get the most recent value.

How would you do it?

The desired table:

id    value      datetime
1     5          1285891200    // Today
2     18         1285804800    // Yesterday

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-17T02:17:02+00:00Added an answer on May 17, 2026 at 2:17 am

    Gotta love MySQL for allowing an order by in a subquery. That’s not allowed by the SQL standard 🙂

    You could rewrite the query in a standards complaint way like:

    select  *
    from    YourTable a
    where   not exists
            (
            select  *
            from    YourTable b
            where   a.id = b.id
            and     a.datetime < b.datetime
            )
    

    In case there are duplicates that you can’t split apart in the subquery, you can group by and then pick an arbitrary value:

    select  a.id
    ,       max(a.value)
    ,       max(a.datetime)
    from    YourTable a
    where   not exists
            (
            select  *
            from    YourTable b
            where   a.id = b.id
            and     a.datetime < b.datetime
            )
    group by
            a.id
    

    This chooses the maximum a.value sharing the latest datetime. Now datetime is the same for all duplicate rows, but standard SQL doesn’t know that, so you have to specify a way to pick from the equal days. Here, I’m using max, but min or even avg would work just as well.

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

Sidebar

Related Questions

CGI.escapeHTML is pretty bad, but CGI.unescapeHTML is completely borked. For example: require 'cgi' CGI.unescapeHTML('&#8230;')

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.