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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:07:23+00:00 2026-06-07T23:07:23+00:00

Somewhat confusing so its easier if I put down example and expected output to

  • 0

Somewhat confusing so its easier if I put down example and expected output to begin.

I have a table that could look like this: (Unit1 – Unit2 columns could span up to 30 columns in the same general format)

| ID | Name | Unit1_left | Unit2_left |
| 1  | Tom  |     50     |     NULL   |
| 2  | Tom  |    NULL    |      1     |
| 3  | Tom  |     45     |     NULL   |
| 4  | Dan  |    NULL    |     NULL   |

What I am trying to select is a table like this:

| Name | Unit1_left | Unit2_left |
| Tom  |     45     |      1     |
| Dan  |    NULL    |     NULL   |

What that is doing is grouping by name and attempting to find the last values in the 2 other columns if they exist (if not then it returns NULL).

I have looked at various other questions and they all say to use Max() however this will not work since it selects the highest value (incorrect). I have seen that in MsSQL there is a Last() function which looks vaguely like what I want it to do but its not implemented in MySQL and isn’t exactly what I need anyway.

What I am trying to ask is, does anyone know of a possible method of selecting the data like this or if I will have to use a separate programming language to do this?

  • 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-06-07T23:07:25+00:00Added an answer on June 7, 2026 at 11:07 pm

    This will produce the result set you’ve described

    SELECT dname.name, 
           l1value.unit1_left, 
           l2value.unit2_left 
    FROM   (SELECT DISTINCT `name` 
            FROM   table1) `DName` 
           LEFT JOIN (SELECT `name`, 
                             Max(id) id 
                      FROM   table1 
                      WHERE  unit1_left IS NOT NULL 
                      GROUP  BY `name`) l1 
                  ON dname.`name` = l1.`name` 
           LEFT JOIN table1 l1value 
                  ON l1.id = l1value.id 
           LEFT JOIN (SELECT `name`, 
                             Max(id) id 
                      FROM   table1 
                      WHERE  unit2_left IS NOT NULL 
                      GROUP  BY `name`) l2 
                  ON dname.`name` = l2.`name` 
           LEFT JOIN table1 l2value 
                  ON l2.id = l2value.id ;
    

    DEMO

    I did it by creating 2 inline views to the highest id for non-null values for both unit1_left and unit2_left (l1 and l2). Then joined it back to original table to get the values (l1value and l2value). We then join that back to a third inline view (dname) that creates the distinct names.

    It’s quite messy and it might make more sense just to keep your data in a more sensible manner.

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

Sidebar

Related Questions

I have a (somewhat) large truth table / state machine that I need to
I know the title of this post is somewhat confusing, but I could not
I have a horizontal LinearLayout that's programmatically filled with its children. I would like
That's somewhat confusing. NSMutableString inherits from NSString, but does that also mean that I
I realize that the title may be somewhat confusing, so I apologize. Basically, this
I know the title looks somewhat confusing so let me explain. :) I have
I know the title is somewhat confusing, my problem is this: I want to
I am looking to fix my layout of my website. It is somewhat confusing,
Somewhat similar to this question , except we haven't decided that we're going with
This is somewhat complex (well to me at least). Here is what I have

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.