1

Input:

postgres=> select sets from matches limit 2;

         sets         
----------------------
 {{6,4},{6,2}}
 {{6,3},{5,7},{10,4}}

(2 rows)

Expected (Sum of the value in each array)

postgres=> select sets from matches limit 2;

         sets         
----------------------
 18
 33

(2 rows)

I tried, but it only gives me the value of two first value in the first sub array:

postgres=> select unnest(sets) from matches limit 2;

 unnest 
--------
      6
      4

(2 rows)

2 Answers 2

2

You can use a scalar sub-select:

select (select sum(i)
        from unnest(sets) as t(i)) as sum
from matches;
Sign up to request clarification or add additional context in comments.

Comments

0

I haven't worked with postgres in awhile but maybe you're missing the Sum part?:

SELECT
  sum(a) AS total
FROM
  (
    SELECT
      unnest(sets from matches) AS a
  ) AS b

Comments

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.