Domanda

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?

È stato utile?

Soluzione

Try using MAX, like this:

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

GROUP BY email

Altri suggerimenti

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top