Move from rows to columns in Postgresql

0

I have a table very similar to this one (the id is not a primary key):

    id    nombre producto    atributo
    1     Camiseta           Color: Negro
    1     Camiseta           Tam: XL

I would like to know how I can move from the previous table to one like this:

    id    nombre producto    atributos
    1     Camiseta           Color: Negro, Tam: XL

I would also like to know if it is possible to get to this:

    id    nombre producto    atributo1       atributo2
    1     Camiseta           Color: Negro    Tam: XL

One thing to keep in mind is that not all products have an attribute, some can have an attribute, two, or fifteen.

I've tried it with crosstab but I do not know how to use the function. Greetings and thanks.

    
asked by Joshua 14.11.2017 в 21:27
source

1 answer

1
select string_agg(col,',') from my_table

Example:

select id, nombre_producto, string_agg(atributo,',')
from productos where nombre_producto='Camiseta'

I would give you:

id  nombre_producto string_agg
1   Camiseta        Color: Negro, Tam: XL
    
answered by 14.11.2017 в 21:35