1

Source Table with same Id need to combine them into single row

            +-----+-----+-----+-----+
            | 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 |
            +-----+-----+-----+-----+
Dale K
  • 25,246
  • 15
  • 42
  • 71
Mike Swift
  • 81
  • 2
  • 7
  • Hint: Use `MAX`. – Thom A Jan 18 '22 at 18:22
  • Does this answer your question? [Combine multiple rows into one row to replace null values in columns](https://stackoverflow.com/questions/35611273/combine-multiple-rows-into-one-row-to-replace-null-values-in-columns) – Stu Jan 18 '22 at 18:23

2 Answers2

4

A simple aggregation should do the trick

Example

Select ID
      ,ID1 = max(ID1)
      ,ID2 = max(ID2)
      ,ID3 = max(ID3)
 From  YourTable
 Group By ID
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
0

Please check this link - MySQL get first non null value after group by

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

GROUP BY id
frangly
  • 162
  • 7