1
key            | value            | col
----------------------------------------
color          | red              | g1
color          | blue             | g2
contrast       | {'l':123456}     | g1
setting        | srnew            | g2
contrast       | {'s':123456}     | g1    

key        | value                    | col
---------------------------------------------
color      | ['red']                  | g1
color      | ['blue']                 | g2
contrast   | [{'l':123456}, {'s':456}]| g1
setting    | ['srnew']                | g2

How would I aggregate the first table into the second table?

Column value is of type jsonb. I want the result column value to be jsonb too.

0

1 Answer 1

2

You can try to use array_agg do group by concat then array_to_json to json array.

SELECT array_to_json(array_agg(value)) 
FROM t
group by key,col
Sign up to request clarification or add additional context in comments.

2 Comments

No problem glad to help
i replaced the array_to_json(array_agg(_)) with jsonb_agg. because i wanted a jsonb as the column type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.