Postgresql语句以列方式表示重复的行

Postgresql statement to represent rows duplicate in column way

我的数据如下:

Member_ID Housing_ID National_ID Family_relation
1 1 3214565 Head
2 1 1234567 Wife
3 1 1223344 Sun
4 1 3224451 Sun
5 1 5432175 Daughter
1 2 1223344 Head
2 2 4321678 Wife
3 2 4356723 Sun
1 3 3214565 Head
2 3 1234557 Brother
1 4 7653432 Head
2 4 3224451 Grand daughter

如您所见,在不同的房屋中存在身份证件的重复:

1- the member 3 of house 1 with National ID 1223344 (sun) is (head) of house 2, 
2- the member 4 of house 1 with National ID 3224451 (sun) is (Grand daughter) of house 4

我必须指定所有重复的场景才能将其删除,我通过键入以下内容来识别重复:select National ID , count(*) from my table group by National ID having count(*)>1 并得到以下输出:

National ID count
1223344 2
3224451 2

我懒得查询重复项,所以输出看起来像:

National ID Housing ID 1 Relation Housing ID 2 Relation
1223344 1 sun 2 Head
3224451 1 sun 4 Grand daughter

谢谢

对于 count(*) = 2 的 National_ID,以下查询应提供预期结果:

select National ID
     , array_agg(Housing_ID)[1] AS "Housing ID 1"
     , array_agg(Family_relation)[1] AS "Relation 1"
     , array_agg(Housing_ID)[2] AS "Housing ID 2"
     , array_agg(Family_relation)[2] AS "Relation 2"
  from my table 
 group by National_ID 
having count(*) = 2