I have a table of rows with the following structure name TEXT, favorite_colors TEXT[], group_name INTEGER
where each row has a list of everyone's favorite colors and the group that person belongs to. How can I GROUP BY group_name
and return a list of the most common colors in each group?
Could you do a combination of int[] && int[]
to set for overlap, int[] & int[]
to get the intersection and then something else to count and rank?
Quick and dirty:
Better with a
LATERAL JOIN
In Postgres 9.3 or later this is the cleaner form:
The above is shorthand for
And like with any other
INNER JOIN
, it would exclude rows without color (favorite_colors IS NULL
) - as did the first query.To include such rows in the result, use instead:
You can easily aggregate the "most common" colors per group in the next step, but you'd need to define "most common colors" first ...
Most common colors
As per comment, pick colors with > 3 occurrences.
To aggregate the top colors in an array (in descending order):
-> SQLfiddle demonstrating all.