39

I have a large table with data that is not unique but needs to be. This table is a result of multiple union selects so is not an actual table. I cannot make it an actual table for other reasons.

All of the UNION'd tables have an email column which will eventually be unique. The resulting records look like this:

1   ozzy@test.com   Ozzy
2   test@test.com   Tony
3   test@yahoo.com  Steve
4   tiny@test.com   
13  tony@gmail.com  Tony
14  test@test.com   Ozzy
15  test@yahoo.com  Dave
16  tiny@test.com   Tim

As you can see, some emails appear more then once with different names or non-existent names. When I add a GROUP BY email clause at the end, the results look like this:

1   ozzy@test.com   Ozzy
2   test@test.com   Tony
3   test@yahoo.com  Steve
4   tiny@test.com   
13  tony@gmail.com  Tony

As you can see, email 4 does not have a name because it chose the first entry with NULL for a name. Then I tried to use GROUP_CONCAT which made the results look like this:

1   ozzy@test.com   Ozzy
14  test@test.com   Ozzy,Tony
15  test@yahoo.com  Dave,Steve
16  tiny@test.com   Tim
13  tony@gmail.com  Tony

As you can see, now everyone has a name but some rows have more then one name concatenated. What I want to do is GROUP BY email and choose the first NOT NULL entry of each column for each row to theoretically look like so:

1   ozzy@test.com   Ozzy
2   test@test.com   Tony
3   test@yahoo.com  Steve
4   tiny@test.com   Tim
13  tony@gmail.com  Tony

I have tried using COALESCE but it doesn't work as intended. My current query looks like so:

SELECT
    id,
    email,
    `name`
FROM
(
    SELECT
        email,
        `name`
    FROM
        multiple_tables_and_unions
) AS emails

GROUP BY email

I have removed the code from the temporary table as it contains many tables but all select the email and name column. Essentially I need a function like GROUP_COALESCE but unfortunately it does not exist. What are my options?

2 Answers 2

56

Try using MAX, like this:

SELECT
    email,
    MAX(`name`)
FROM
(
    SELECT
        email,
        `name`
    FROM
        multiple_tables_and_unions
) AS emails

GROUP BY email
6
  • 3
    Sometimes the simplest answer is the best :) I always assumed MAX worked only on numbers. Thanks
    – Ozzy
    Apr 1, 2014 at 10:45
  • Smart one! Like it
    – Demo
    Dec 7, 2017 at 2:02
  • little nugget. old but gold
    – digout
    May 22, 2019 at 6:20
  • 1
    But will this pick the first non null value? I doubt! It will pick the max value order by alphabet. Also it will do the same if we have int value instead of string. Mar 8, 2021 at 5:48
  • @JigneshM.Khatri the OP wanted to skip NULLs and pick a non-NULL value instead, didn't specifically wanted the MAX value. Mar 8, 2021 at 9:57
2

To select the first non null value, GROUP_CONCAT can be handy here. Below is the example:

SELECT
    email,
    SUBSTRING_INDEX(GROUP_CONCAT(`name`), ',',1)
FROM
(
    SELECT
        email,
        `name`
    FROM
        multiple_tables_and_unions
) AS emails

GROUP BY email;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.