2

I have a postgres table like this

activity

comment    likes
Hi         ["bill", "susan"]
Hello      ["susan", "john", "joe"]

I'm trying to get the total number of likes.

Likes is of type jsonb

I know I could select all the data and then loop through it with an increment and count it, but that would create performance issues once you get a lot of data.

Is there a way to do it with a single query?

For the above example, the total number of likes should be 5

1 Answer 1

1

The answer from @dassum was close, I just needed to adjust it to support jsonb

SELECT SUM(jsonb_array_length(likes)) FROM activity;

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, I missed jsonb. the good thing is your issue got resolved.

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.