在 PostgreSQL 中旋转 table

Pivoting a table in PostgreSQL

db 新手,我有以下 table,我需要将列值转换为行,有人可以帮忙吗。

下面是我已有的源数据:

id              custom_key  custom_value
648079          WrapUpName  Default Wrap-up Code
648079          agentEmail  abc@gmail.com
648079          Location    Hyderabad
648079          Supervisor  Vinay
648079          Publication xyz
648122          WrapUpName  Default Wrap-up
648122          agentEmail  efg@gmail.com
648122          Location    Mumbai
648122          Supervisor  Pranay
648122          Publication mnp

我需要使用 PostgreSQL 查询将以上值转换为以下格式。

id              WrapUpName              agentEmail      Location    Supervisor  Publication
648079          Default Wrap-up Code    abc@gmail.com   Hyderabad   Vinay       xyz
648122          Default Wrap-up Code    efg@gmail.com   Mumbai      Pranay      mnp

只使用条件聚合:

select id,
    max(custom_value) filter(where custom_key = 'WrapUpName') as wrap_up_name,
    max(custom_value) filter(where custom_key = 'agentEmail') as agent_email
    ...
from mytable
group by id