Home > OS >  Postgres turn join rows into columns
Postgres turn join rows into columns

Time:01-18

I have this database schema:

drop table if exists demo_contact cascade;
create table demo_contact (id serial primary key);
insert into demo_contact values (1);
insert into demo_contact values (2);
insert into demo_contact values (3);

drop table if exists demo_contact_custom_field cascade;
create table demo_contact_custom_field (custom_field_id text, contact_id numeric, value text);
insert into demo_contact_custom_field values ('7759512f-662f-4139-94fb-8b708c5d11eb', 1, '3232');
insert into demo_contact_custom_field values ('a96993bf-eb38-446c-a5a7-416485e8b933', 1, 'true');
insert into demo_contact_custom_field values ('a96993bf-eb38-446c-a5a7-416485e8b933', 2, 'true');

How can I produce this sort of output?

contact_id 7759512f-662f-4139-94fb-8b708c5d11eb a96993bf-eb38-446c-a5a7-416485e8b933
1 3232 true
2 true

I searched around for various queries relating to transposing a table, pivot tables in Postgres, this literal question title "turn postgres join rows into columns" but I haven't found a solution:

  • enter image description here

  • Related