簡體   English   中英

postgres按公共ID在多行數組中分組

[英]postgres group by common id in array on many rows

我有一張這樣的桌子:

{
"type": "FeatureCollection",
"name": "test",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "final_zone": "ECZ", "id": 20753, "ids": "{22210,10959,22209,22213}", "sub": "{ECZECSZ,NULL,ECZECSZ,ECZECSZ}" }, "geometry": null },
{ "type": "Feature", "properties": { "final_zone": "Protection", "id": 6516, "ids": "{24920,24943}", "sub": "{NULL,NULL}" }, "geometry": null },
{ "type": "Feature", "properties": { "final_zone": "Protection", "id": 6524, "ids": "{24912,24920,24943,24971,24944}", "sub": "{NULL,NULL,NULL,NULL,NULL}" }, "geometry": null },
{ "type": "Feature", "properties": { "final_zone": "Protection", "id": 6528, "ids": "{24943,24958,24944}", "sub": "{NULL,NULL,NULL}" }, "geometry": null },
{ "type": "Feature", "properties": { "final_zone": "Protection", "id": 6610, "ids": "{24943,24971}", "sub": "{NULL,NULL}" }, "geometry": null },
{ "type": "Feature", "properties": { "final_zone": "Protection", "id": 6781, "ids": "{24912,24906,24943}", "sub": "{NULL,NULL,NULL}" }, "geometry": null }
]
}

在此特定實例中,所有5行中都存在24943。 我該如何折疊這樣的桌子? 並將陣列匯總為1個平滑陣列。

請記住,還有成千上萬的其他行,所以我不要一個龐大的組。 我只想將ids數組中具有相同公共ID的行折疊起來

在此處輸入圖片說明

我可以做這個

with abid as(
select regexp_replace(id,'[{}]','','gi')::int id from(
    select unnest(ids) id from(
        select string_to_array(ids,',') ids from conflict.test
        )t
    )t2
),
abid2 as(select ids::int[] id from conflict.test
    )
select t2.*,t.* from abid t,abid2 t2 where t.id =any(t2.id)

只是為了給更多的范圍,下面的棕色中間部分的ID為24943 在此處輸入圖片說明

不清楚您想要的結果,如果您只是想統一數組,請嘗試以下操作:

with tmp as (
select unnest(ids) ids
from your_table_name
group by unnest(ids)
)
select x.final_zone, x.id, array_agg(t.ids), x.sub, x.polys
from tmp t
cross join your_table_name x
group by x.final_zone, x.id, x.sub, x.polys;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM