我有下表:
CREATE TABLE person
AS
SELECT name, preferences
FROM ( VALUES
( 'John', ARRAY['pizza', 'meat'] ),
( 'John', ARRAY['pizza', 'spaghetti'] ),
( 'Bill', ARRAY['lettuce', 'pizza'] ),
( 'Bill', ARRAY['tomatoes'] )
) AS t(name, preferences);
Run Code Online (Sandbox Code Playgroud)
我想group by person与intersect(preferences)为聚合函数。所以我想要以下输出:
person | preferences
-------------------------------
John | ['pizza']
Bill | []
Run Code Online (Sandbox Code Playgroud)
这应该如何在 SQL 中完成?我想我需要做类似下面的事情,但是这个X函数是什么样子的?
SELECT person.name, array_agg(X)
FROM person
LEFT JOIN unnest(preferences) preferences
ON true
GROUP BY name
Run Code Online (Sandbox Code Playgroud)