想象一下資料:
id name 2019 2020 2021
1 Ana {fruit,health}
1 Ana {veggie}
2 Bill {beauty}
2 Bill {veggie}
2 Bill {health,veggie}
我的目標是結果:
id name 2019 2020 2021
1 Ana {fruit, health} {veggie}
2 Bill {beauty} {veggie} {health,veggie}
我怎樣才能達到這些結果?我正在努力尋找這樣的案例。
uj5u.com熱心網友回復:
with imagine_the_data(id,name,"2019","2020","2021") AS
(
select 1, 'Ana', '{fruit,health}',null,null union all
select 1,'Ana',null,null,'{veggie}' union all
select 2,'Bill','{beauty}' ,null,null union all
select 2,'Bill',null,'{veggie}' ,null union all
select 2,'Bill' ,null,null,' {health,veggie}'
)
select max(i.id)as id,i.name,max(i."2019")as max_2019,max(i."2020")as max_2020,
max(i."2021")as max_2021
from imagine_the_data i
group by i.name
可能這個適合
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/491552.html
標籤:sql 细绳 PostgreSQL 总计的
下一篇:為什么它告訴我我在比較函式?