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

The Archive Base Latest Questions

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

I am really stuck on this. I’m clearly not understanding the MIN/MAX concept. I

  • 0

I am really stuck on this. I’m clearly not understanding the MIN/MAX concept.

I am trying to get the latest row from a grouping of work_type and work_id.

If I change from MIN to MAX, it changes the returned timestamp, but it never brings the status info from that record.

Example:

"SELECT 
    CONCAT(work_type, work_id) AS condition_id, 
    status,
    MIN(created_timestamp) as latest
  FROM conditions
  GROUP BY condition_id"

With MIN, I get:

    Array
    (
        [0] => Array
            (
                [condition_id] => cutouts00002
                [status] => bad
                [latest] => 2011-02-21 15:20:27
            )

        [1] => Array
            (
                [condition_id] => paintings00002
                [status] => damagez
                [latest] => 2011-02-21 14:43:35
            )

    )

With MAX I get:

Array
(
    [0] => Array
        (
            [condition_id] => cutouts00002
            [status] => bad
            [latest] => 2011-02-21 15:22:20
        )

    [1] => Array
        (
            [condition_id] => paintings00002
            [status] => damagez
            [latest] => 2011-02-21 14:43:41
        )

)

Bu the thing is that the status in the row with the latest timestamp, is “no damage”, but it never returns the row corresponding to the MAX(current_timestamp), it only ever returns the “damagez” row.

Any help is appreciated.

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-20T05:01:51+00:00Added an answer on May 20, 2026 at 5:01 am

    You have fallen prey of the MySQL lax rules that allow for non-aggregates to be included in a GROUP BY query. Sure, you are working with MIN or MAX, and only ONE at a time, but consider this query:

    SELECT 
        CONCAT(work_type, work_id) AS condition_id, 
        status,
        MIN(created_timestamp) as earliest,
        MAX(created_timestamp) as latest
      FROM conditions
      GROUP BY condition_id
    

    Now, think about which row the status column should come from. It’s absurd to put a correlation between the aggregate (those in the GROUP BY) and non-aggregate columns.

    Instead, write your query like this

    SELECT X.condition_id, C.status, X.earliest
    FROM (
      SELECT 
        CONCAT(work_type, work_id) AS condition_id, 
        status,
        MIN(created_timestamp) as earliest
      FROM conditions
      GROUP BY condition_id
    ) X JOIN conditions C
      on CONCAT(c.work_type, c.work_id) = X.condition_id
      and c.created_timestamp = X.earliest
    

    But if you had two records with the same created_timestamp, it gets even more tricky

    SELECT X.condition_id, Max(C.status) status, X.earliest
    FROM (
      SELECT 
        CONCAT(work_type, work_id) AS condition_id, 
        status,
        MIN(created_timestamp) as earliest
      FROM conditions
      GROUP BY condition_id
    ) X JOIN conditions C
      on CONCAT(c.work_type, c.work_id) = X.condition_id
      and c.created_timestamp = X.earliest
    GROUP BY X.condition_id, X.earliest
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm really stuck with this and not able to move forward i'm trying to
I'm really stuck trying to use java wrapper library for opencv's cvMatchTemplate. See this
I am really stuck on this. I am trying to do a simple textswitcher
I'm really stuck on this one... Basically, I'm trying to make 2 pages always
I am really stuck on this one. Website is almost finished. Just trying to
I realize this question is pretty basic, but I'm really stuck. I have a
I have been stuck on this and don't really know how to go about
I've been stuck on this for a couple of days now, and I'm really
I'm really stuck trying to find out how to force a programmatic refresh of
I know this is very common question but I am really stuck in this

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.