Skip to content
Advertisement

Postgres: Split column values & transpose

I have a table like this:

And I want to convert it to this:

I have tried splitting the column based on ; but the product_id count varies based on the category_id. Is there anyway I can achieve this?

Advertisement

Answer

You can use PostgreSQL’s array manipulation functions:

string_to_array does exactly what it says — splits a string into an array of parts using a supplied delimiter, then unnest is used to separate an array value into multiple rows containing elements from the array.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement