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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:53:26+00:00 2026-05-27T13:53:26+00:00

Background My typical use case: # Table id category dataUID ————————— 0 A (NULL)

  • 0

Background

My typical use case:

# Table

id     category    dataUID
---------------------------
0         A        (NULL)
1         B        (NULL)
2         C        text1
3         C        text1
4         D        text2
5         D        text3

# Query

SELECT MAX(`id`) AS `id` FROM `table`
GROUP BY `category`

This is fine; it will strip out any “duplicate categories” in the recordset that’s being worked on, giving me the “highest” ID for each category.

I can then go on use this ID to pull out all the data again:

# Query

SELECT * FROM `table` JOIN (
   SELECT MAX(`id`) AS `id` FROM `table`
   GROUP BY `category`
) _ USING(`id`)

# Result

id     category    dataUID
---------------------------
0         A        (NULL)
1         B        (NULL)
3         C        text1
5         D        text3

Note that this is not the same as:

SELECT MAX(`id`) AS `id`, `category`, `dataUID` FROM `table`
GROUP BY `category`

Per the documentation:

In standard SQL, a query that includes a GROUP BY clause cannot refer
to nonaggregated columns in the select list that are not named in the
GROUP BY clause. For example, this query is illegal in standard SQL
because the name column in the select list does not appear in the
GROUP BY:

SELECT o.custid, c.name, MAX(o.payment)   FROM orders AS o, customers
AS c   WHERE o.custid = c.custid   GROUP BY o.custid;

For the query to be legal, the name column must be omitted from the
select list or named in the GROUP BY clause.

MySQL extends the use of GROUP BY so that the select list can refer to
nonaggregated columns not named in the GROUP BY clause.
This means
that the preceding query is legal in MySQL. You can use this feature
to get better performance by avoiding unnecessary column sorting and
grouping. However, this is useful primarily when all values in each
nonaggregated column not named in the GROUP BY are the same for each
group.

[..]

This extension assumes that the nongrouped columns will have the same group-wise values. Otherwise, the result is indeterminate.

So I’d get an unspecified value for dataUID — as an example, either text2 or text3 for result with id 5.

This is actually a problem for other fields in my real case; as it happens, for the dataUID column specifically, generally I don’t really care which value I get.


Problem

However!

If any of the rows for a given category has a NULL dataUID, and at least one other row has a non-NULL dataUID, I’d like MAX to ignore the NULL ones.

So:

id     category    dataUID
---------------------------
4         D        text2
5         D        (NULL)

At present, since I pick out the row with the maximum ID, I get:

5         D        (NULL)

But, because the dataUID is NULL, instead I want:

4         D        text2

How can I get this? How can I add conditional logic to the use of aggregate MAX?


I thought of maybe handing MAX a tuple and pulling the id out from it afterwards:

GET_SECOND_PART_SOMEHOW(MAX((IF(`dataUID` NOT NULL, 1, 0), `id`))) AS `id`

But I don’t think MAX will accept arbitrary expressions like that, let alone tuples, and I don’t know how I’d retrieve the second part of the tuple after-the-fact.

  • 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-27T13:53:27+00:00Added an answer on May 27, 2026 at 1:53 pm

    slight tweak to @ypercube’s answer. To get the ids you can use

    SELECT COALESCE(MAX(CASE
                          WHEN dataUID IS NOT NULL THEN id
                        END), MAX(id)) AS id
    FROM   table
    GROUP  BY category  
    

    And then plug that into a join

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

Sidebar

Related Questions

First things first, I am a Python beginner, with a typical C++/Java background for
I come from an SVN background so I'm not sure what the typical git
I have a very typical situation. We have a table called Users which has
Background Below is a typical piece of Perl code ( sample.pl for the sake
I'm trying to use one single image sprite for typical male / female radio
Background: I have a little video playing app with a UI inspired by the
Background: At my company we are developing a bunch applications that are using the
Background: Some time ago, I built a system for recording and categorizing application crashes
Background: I need to reserve an amount of memory below 0xA0000 prior to my
Background I am writing and using a very simple CGI-based (Perl) content management tool

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.