Skip to content
Advertisement

Looking for Postgres query which can provide output like MongoDB group by function

Product table

Size table

Query:

Current output:

Expected output

Note: Due to current query I’m getting 2 record for the same product and my limit and offset query logic is getting false and not getting proper count. I’m not well aware of Postgres queries for this kind of situation. So I need solution for this so my limit and offset logic will be correct for fetching data and for this query my count of product will be 1.

Advertisement

Answer

Use array_agg():

The ORDER BY within the aggregation is optional, but it’s always nice to get consistent results over and over again.

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