问题
I have a column of text[]. How do I get a frequency count of all the objects across the column?
Example:
col_a
--------
{a, b}
{a}
{b}
{a}
Output should be:
col_a | count
----------------
a | 3
b | 2
My query:
with all_tags as (
select array_agg(c)
from (
select unnest(tags)
from message_tags
) as dt(c)
)
select count(*) from all_tags;
回答1:
figured it out:
-- Collapse all tags into one array
with all_tags as (
select array_agg(c) as arr
from (
select unnest(ner_tags)
from message_tags
) as dt(c)
),
-- Expand single array into a row per tag
row_tags as (
select unnest(arr) as tags from all_tags
)
-- count distinct tags
select tags, count(*) from row_tags group by tags
来源:https://stackoverflow.com/questions/51486004/postgres-frequency-count-across-arrays