Desde su estado actual, simplemente puede hacer el pivote usando el FILTER
cláusula:
SELECT
response,
document,
MAX(bill) FILTER (WHERE label = 'bill') as bill,
MAX(answer) FILTER (WHERE label = 'amount') as amount,
MAX(product) FILTER (WHERE label = 'product') as product,
MAX(answer) FILTER (WHERE label = 'price') as price
FROM t
GROUP BY response, document
No estoy muy seguro de cómo se ve tu tabla original. Si es más como esto:
response | document | label | value
-------: | -------: | :------ | :----
71788176 | 79907201 | bill | 26899
71788176 | 79907201 | amount | 1
71788176 | 79907201 | product | shoes
71788176 | 79907201 | price | 25.99
Luego puede modificar la consulta de esta manera:
SELECT
response,
document,
MAX(value) FILTER (WHERE label = 'bill') as bill,
MAX(value) FILTER (WHERE label = 'amount') as amount,
MAX(value) FILTER (WHERE label = 'product') as product,
MAX(value) FILTER (WHERE label = 'price') as price
FROM t
GROUP BY response, document
Editar :TO agregó el valor JSON a la columna del producto:
Variante 1:simplemente puede emitir el tipo json
en escriba text
:
MAX(product::text) FILTER (WHERE label = 'product') as product,
Variante 2:Lees el valor del "name"
atributo:
MAX(product ->> 'name') FILTER (WHERE label = 'product') as product,