Skip to content
Advertisement

SQL return max in unnest

I have the following query:

SELECT
table_a.col1,
table_a.col2,
array.col1
FROM
table_a
LEFT JOIN UNNEST(table_a.array) AS array

What should I do if I only want the query to return one array.col1 per row in table_a (let’s say I want only the max value for each case)?

Advertisement

Answer

You can use a subquery:

select a.*,
       (select max(el)
        from unnest(a.array) el
       ) as max_el
from table_a a;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement