【问题标题】:Merging many unique rows into one将许多独特的行合并为一个
【发布时间】:2017-06-30 05:58:37
【问题描述】:

我正在努力创建一个选择查询,该查询将每一行合并/聚合为按列 postfix_id 分组的单行。

|   postfix_id  |   initial_datetime  |    final_datetime   |  from_address  |   to_address   | response |

|   655CB4B10   | 2017-06-30 00:17:13 |         null        |      null      |      null      |   null   |
|   655CB4B10   |         null        |         null        | bob@domain.tld |      null      |   null   |
|   655CB4B10   |         null        |         null        |      null      | sam@domain.tld |   null   |
|   655CB4B10   |         null        | 2017-06-30 00:17:14 |      null      |      null      |   sent   |
|   C32AE57F3   | 2017-06-10 07:14:36 |         null        |      null      |      null      |   null   |
|   C32AE57F3   |         null        |         null        | joe@domain.tld |      null      |   null   |
|   C32AE57F3   |         null        |         null        |      null      | tye@domain.tld |   null   |
|   C32AE57F3   |         null        | 2017-06-10 07:22:54 |      null      |      null      |  bounce  |

每个 postfix_id 值被列出 4 次,我需要将每个匹配项合并到一行中,从而删除剩余列中存在的空值。

例如所需的输出:

|   postfix_id  |   initial_datetime  |    final_datetime   |  from_address  |   to_address   | response |

|   655CB4B10   | 2017-06-30 00:17:13 | 2017-06-30 00:17:14 | bob@domain.tld | sam@domain.tld |   sent   |
|   C32AE57F3   | 2017-06-10 07:14:36 | 2017-06-10 07:22:54 | joe@domain.tld | tye@domain.tld |  bounce  |

该表有超过一百万个独特的 postfix_id,我浏览过其他类似的帖子,但是,它们有要比较的列和/或不相关的列。我无数次使用 MAX 和 GROUP_CONCAT 的尝试都失败了。

任何帮助/指导将不胜感激。我觉得好像很简单,导致 /facepalm


再次感谢,我最终得到了以下内容以满足我的最终需求。我希望这对以后的人有所帮助..

SELECT postfix_id,from_address,to_address,remote_response,insert_datetime,initial_datetime,final_datetime
FROM (
SELECT
postfix_id,
MAX(from_address)     AS from_address,
MAX(to_address)       AS to_address,
MAX(remote_response) AS remote_response,
MAX(insert_datetime)   AS insert_datetime,
MAX(initial_datetime) AS initial_datetime,
MAX(final_datetime)   AS final_datetime
FROM email_outbound_postfix
GROUP BY
postfix_id
) as t1
WHERE insert_datetime >= DATE_SUB(NOW(),INTERVAL 1 DAY)
AND CONCAT(from_address, ' ', to_address) LIKE '%sam%';

【问题讨论】:

    标签: mysql sql merge subquery


    【解决方案1】:

    如果您向我们展示的数据准确无误,那么您只需在postfix_id 列上汇总您的表格,然后选择其他所有列的MAX()

    SELECT
        postfix_id,
        MAX(initial_datetime) AS initial_datetime,
        MAX(final_datetime)   AS final_datetime,
        MAX(from_address)     AS from_address,
        MAX(to_address)       AS to_address
    FROM yourTable
    GROUP BY
        postfix_id
    

    这应该起作用的原因是MAX() 忽略 NULL 列中的值。所以假设每个postfix_id 组只有一个非NULL 值,那么MAX() 会选择它。

    输出:

    演示在这里:

    Rextester

    【讨论】:

    • 感谢您的详细回复。在发布之前我有一些非常相似的东西,但是我遇到了一个奇怪的问题,其中 final_datetime、to_address 和 remote_response 在输出中为空。我可能有一些奇怪的东西导致了冲突。现在看..
    • @cEMA 也许您的源数据有问题。即使给定列的每组记录不只有一个非NULL 值,我们仍然可以编写查询。
    • 如果没有看到您的数据,恐怕我无能为力。检查输出并尝试查看哪些组有问题。仅仅因为每个组有多个非 NULL 值并不意味着查询不会运行,而只是意味着您可能无法获得预期的输出。
    • 我相信 WHERE 语句导致了我的问题。非常感谢您的帮助!
    【解决方案2】:

    为此你可以简单地使用Mysql的MAX

     SELECT postfix_id , MAX(initial_datetime) AS initial_datetime ,MAX(final_datetime) AS final_datetime ,MAX(from_address) AS  from_address ,MAX(to_address) AS to_address,MAX(response) AS response from table group by postfix_id 
    

    有关 MAX 的更多信息,请阅读https://dev.mysql.com/doc/refman/5.7/en/example-maximum-column.html

    【讨论】:

    • 非常感谢您的回复(也很快!)。 Tim 包含了更多细节,还链接到了我以前从未见过的非常有用的 Rextester 网站;所以我不得不把它给他。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    相关资源
    最近更新 更多