0

I have a Postgresql JSON 2D array column containing string terms, e.g.:

Input

[["edwards", "block", "row"], ["edwards"], ["block"]]

Is it possible to compute the occurrence of each term purely in Postgresql? e.g.:

Output

Terms,                        Occur
["edwards", "block", "row"]   [2,2,1]

(Or in some similar format). Or would I have to compute the occurrences using a programming language?

2 Answers 2

1

Here's one solution:

select array_to_json(array(
    select json_array_length(a.value)
    from json_array_elements('[["edwards", "block", "row"], ["edwards"], ["block"]]'::json) a
));
Sign up to request clarification or add additional context in comments.

Comments

0

Figured this one out eventually. The query is as follows:

SELECT COUNT(t), t
FROM (SELECT jsonb_array_elements_text(json->'named_entities') AS t
FROM tweets WHERE event_id = XX) AS t1
GROUP BY t

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.