爲組聚合函數I具有下表:陣列相交通過
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);
欲group by person
與intersect(preferences)
作爲聚合函數。所以我想要以下輸出:
person | preferences
-------------------------------
John | ['pizza']
Bill | []
這應該如何在SQL中完成?我想我需要做類似以下的事情,但X
函數是什麼樣的?
SELECT person.name, array_agg(X)
FROM person
LEFT JOIN unnest(preferences) preferences
ON true
GROUP BY name
可能會加入unnest(首選項)? –
@VaoTsun我認爲這是一個好主意,但我該如何與該連接相交(並在之後應用'array_agg')? –
數組有重複值的機會嗎? –