Postgresql Group by array elements in common


November 2018


318 time


I have a table like this:

CREATE TABLE preferences (name varchar, preferences varchar[]);
INSERT INTO preferences (name, preferences) 
    ('John','{pizza, spaghetti}'), 
    ('Charlie','{spaghetti, rice}'), 
    ('Lucy','{rice, potatoes}'), 
    ('Beth','{bread, cheese}'), 
    ('Trudy','{rice, milk}');

So from the table

John      {pizza, spaghetti}
Charlie   {spaghetti, rice}
Lucy      {rice, potatoes}
Beth      {bread, cheese}
Trudy     {rice, milk}

I would like group all rows that have elements in common (even if it is through other people). So in this case I would like to end up with:

{John,Charlie,Lucy,Trudy}     {pizza,spaghetti,rice,potatoes,milk}
{Beth}                        {bread, cheese}

because Johns preferences intersect with those of Charlie, and those of Charlie intersect with those of Lucy and with those of Trudy.

I already haven an array_intersection function like this:

CREATE OR REPLACE FUNCTION array_intersection(anyarray, anyarray)
  RETURNS anyarray
  language sql
        SELECT UNNEST($1)
        SELECT UNNEST($2)

and know the array_agg function to aggregate arrays, but how to turn those into a grouping like I want is the step I am missing.

1 answers


Это типичная задача для рекурсии. Вам нужно вспомогательную функцию, для объединения и сортировки двух массивов:

create or replace function public.array_merge(arr1 anyarray, arr2 anyarray)
    returns anyarray
    language sql immutable
as $function$
    select array_agg(distinct elem order by elem)
    from (
        select unnest(arr1) elem 
        select unnest(arr2)
    ) s

Используйте функцию рекурсивного запроса:

with recursive cte(name, preferences) as (  
    select *
    from preferences
    select, array_merge(c.preferences, p.preferences)
    from cte c
    join preferences p 
    on c.preferences && p.preferences 
    and <>
select array_agg(name) as names, preferences
from (
    select distinct on(name) *
    from cte
    order by name, cardinality(preferences) desc
    ) s
group by preferences;

           names           |             preferences              
 {Charlie,John,Lucy,Trudy} | {milk,pizza,potatoes,rice,spaghetti}
 {Beth}                    | {bread,cheese}
(2 rows)