简体   繁体   English

将具有相同 ID 名称的两行合并为一行

[英]Combine two rows in a single row with the same ID name

Source Table with same Id need to combine them into single row具有相同 Id 的源表需要将它们组合成单行

            +-----+-----+-----+-----+
            | Id  | ID1 | ID2 | ID3 |
            +-----+-----+-----+-----+
            | XYZ |     |   1 |   3 |
            | ZZZ |   4 |     |   5 |
            | ZZZ |     |   6 |     |
            | XYZ |   8 |    |     |
            +-----+-----+-----+-----+

What I want to achieve:我想要达到的目标:

            +-----+-----+-----+-----+
            | Id  | ID1 | ID2 | ID3 |
            +-----+-----+-----+-----+
            | XYZ |   8 |   1 |   3 |
            | ZZZ |   4 |   6 |   5 |
            +-----+-----+-----+-----+

A simple aggregation should do the trick一个简单的聚合应该可以解决问题

Example例子

Select ID
      ,ID1 = max(ID1)
      ,ID2 = max(ID2)
      ,ID3 = max(ID3)
 From  YourTable
 Group By ID

Please check this link - MySQL get first non null value after group by请检查此链接 - MySQL 在分组后获得第一个非 null 值

SELECT
    id,
    MAX(id1),
    MAX(id2),
    MAX(id3)
FROM
(
    SELECT
        id, id1, id2,id3
    FROM
        Test
) AS t

GROUP BY id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM