Actually, these data are the aggregated characteristics of goods for a specific product category, and are used to build filters for the Internet catalog. After crawling all the products in the database, I have a table with unique pairs:

attr , value

brand samsung

brand, huawei

brand, htc

color, white

color black

color red

color pink

color, orange

color, olive

type, slim

type, ultraslim

Where attr and value are VARCHAR data (40)

Using the capabilities of Postgres to work with arrays, I need to present this data in a format that is easy to understand by the Internet directory (the data will be rendered in HTML form with selects):

attr , values

brand, (samsung, huawei, htc)

color, (white, black, red, pink, orange, olive)

type, (slim, ultraslim)

Where values is an array of type VARCHAR (40) []

How to implement this conversion using standard Postgres tools?

    1 answer 1

    Try this:

    SELECT attr ,array_agg("value") "values" FROM table GROUP BY attr 

    The values will be an array. Here is a description of the aggregation functions. Here is a description of the functions of working with arrays.

    • Great, it works, thank you - Pifagorych