2

I've a table with the following structure:

user  |  medias
----------------------
 1    | {ps2,xbox}
 1    | {nintendo,ps2}

How do i count the occurrences of each string in an array column?

Expected result:

media    |  amount
------------------
ps2      |    2
nintendo |    1
xbox     |    1

1 Answer 1

1

You can unnest the array with a lateral join, then aggregate:

select x.media, count(*) amount
from myable t
cross join lateral unnest(t.medias) x(media)
group by x.media
order by amount desc, x.media
Sign up to request clarification or add additional context in comments.

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.