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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:39:15+00:00 2026-05-20T05:39:15+00:00

I feel there is a simple solution to this — I’ve looked at other

  • 0

I feel there is a simple solution to this — I’ve looked at other questions on Stack Overflow, but they seem to be inefficient, or perhaps I’m doing them wrong.

Here are simplified versions of tables I’m working with.

CREATE TABLE object_values (
  object_value_id INT PRIMARY KEY,
  object_id INT,
  value FLOAT,
  date_time DATETIME,
  INDEX (object_id)
);

CREATE TABLE object_relations (
  object_group_id INT,
  object_id INT,
  INDEX (object_group_id, object_id)
);

There is a many-to-one relationship — many object values per one object (150 object values on average per object)

I want to get the last object value (determined by date_time field) for each object_id based on the object_group_id.

Current Query:

SELECT a.`object_id`, a.`value` 
FROM `object_values` AS a 
    LEFT JOIN `object_relations` AS b 
        ON ( a.`object_id` = b.`object_id` ) 
WHERE b.`object_group_id` = 105 
ORDER BY a.`date_time` DESC

This pulls back all the records — if I do a GROUP BY a.object_id then the ORDER BY gets ignored

I have tried a series of variations — and I apologize as I know this is a common question, but trying the other solutions hasn’t quite worked so far. Any help would greatly appreciated.

Some of the solutions came back with results about 70 seconds later — this is a large system so needs to be a bit faster.

  • 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-20T05:39:16+00:00Added an answer on May 20, 2026 at 5:39 am
    SELECT a.object_id, a.`value`
    FROM object_values a JOIN object_relations b
        ON a.object_id = b.object_id
        JOIN (
        SELECT a.object_id, MAX(a.date_time) MaxTime
        FROM object_relations b JOIN object_values a
            ON a.object_id = b.object_id
        WHERE b.`object_group_id` = 105 
        GROUP BY a.object_id
      ) g ON a.object_id = g.object_id AND a.date_time = g.MaxTime
    WHERE b.`object_group_id` = 105 
    GROUP BY a.object_id
    

    You can omit the final GROUP BY if there will never be duplicate date_time in a single group.

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

Sidebar

Related Questions

I feel like there's a simple solution to this, but I'm not really sure
I feel like there is a simple solution here, but I just can't seem
I feel like there's a pretty simple way to do this, but I'm not
In my heart, I feel that there must be a super simple recursive solution
I've been thinking of implementing this system, but can't help but feel there's a
I'm sure that there's a really simple solution to this that will make me
I'm fairly sure this has a simple solution, but I haven't been able to
There probably is a fairly simple and straight-forward answer for this, but for some
I just had the jQuery epiphany the other day and still feel like there
I feel stupid for asking, but there must be a one liner that does

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.