根据另一列合并一列中的值

Coalesce values in one column based on another column

如何合并由另一列分组的列中的值?

例如如何通过第 1 列值合并第 2 列值?于是转下:

Column 1|Column 2
-----------------
A       |Bob
A       |
B       |Mary
C       |
C       |Kevin
C       |
D       |
D       |

进入这个:

Column 1|Column 2
-----------------
A       |Bob
B       |Mary
C       |Kevin
D       |

谢谢!

使用聚合。这是一种方法:

select col1, max(col2)
from t
group by col1;
select
  col1, 
  unnest(
    coalesce(
      (array_agg(distinct col2) filter (where col2 is not null)),
      '{null}'
    )
  ) 
from t 
group by col1;

更复杂,但也适用于

等数据
Column 1|Column 2
-----------------
A       |Bob
A       |Marley
B       |Mary
C       |
C       |Kevin
C       |Costner
D       |
D       |