Array intersection as aggregate function for group by

Katrine Bers

I have the following table:

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);

I want to group by person with intersect(preferences) as aggregate function. So I want the following output:

person | preferences
-------------------------------
John   | ['pizza']
Bill   | []

How should this be done in SQL? I guess I need to do something like the following, but what does the X function look like?

SELECT    person.name, array_agg(X)
FROM      person
LEFT JOIN unnest(preferences) preferences
ON        true
GROUP BY  name
Evan Carroll

Using FILTER with ARRAY_AGG

SELECT name, array_agg(pref) FILTER (WHERE namepref = total)
FROM (
  SELECT name, pref, t1.count AS total, count(*) AS namepref
  FROM (
    SELECT name, preferences, count(*) OVER (PARTITION BY name)
    FROM person
  ) AS t1
  CROSS JOIN LATERAL unnest(preferences) AS pref
  GROUP BY name, total, pref
) AS t2
GROUP BY name;

Here is one way to do it using the ARRAY constructor and DISTINCT.

WITH t AS (
  SELECT name, pref, t1.count AS total, count(*) AS namepref
  FROM (
    SELECT name, preferences, count(*) OVER (PARTITION BY name)
    FROM person
  ) AS t1
  CROSS JOIN LATERAL unnest(preferences) AS pref
  GROUP BY name, total, pref
)
SELECT DISTINCT
  name,
  ARRAY(SELECT pref FROM t AS t2 WHERE total=namepref AND t.name = t2.name)
FROM t;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Keep Column which is not in aggregate function in group by statement

分類Dev

Postgres SQL: column must appear in the GROUP BY clause or be used in an aggregate function

分類Dev

Rails: must appear in the GROUP BY clause or be used in an aggregate function

分類Dev

Array of objects intersection

分類Dev

Mongodb Aggregate Nested Group

分類Dev

Mongodb Aggregate Nested Group

分類Dev

Group by / Aggregate Pandas Dataframe

分類Dev

SQL GROUP BY Aggregate

分類Dev

Aggregate function, group by clause error when summing difference between two columns

分類Dev

MySQL aggregate function against different groups which should be computed from an outermost GROUP BY?

分類Dev

How to group on a column, array aggregate on another and create a single JSON object keyed by the grouped column

分類Dev

Passing an array of arrays into lodash intersection

分類Dev

Querying MongoDB collection by array intersection

分類Dev

R Looping aggregate by group count

分類Dev

Mongo multiple $lookup and $group in aggregate

分類Dev

SQL Group By + Aggregate functions with joins

分類Dev

MongoDB: aggregate and group by splitting the id

分類Dev

Group by and Aggregate dataset using Python

分類Dev

Subtract group aggregate values in MongoDB

分類Dev

Using aggregate in a function

分類Dev

Problems with SQL sum aggregate function

分類Dev

MongoDB Aggregate function to C#

分類Dev

MongoDB Aggregate Array with Two Fields

分類Dev

MySQL Aggregate Functions without GROUP BY clause

分類Dev

Spark - group and aggregate only several smallest items

分類Dev

How to fix aggregate functions are not allowed in GROUP BY?

分類Dev

Mongodb aggregate query using group-by

分類Dev

MongoDb aggregate and group by two fields depending on values

分類Dev

How to renumber result of intersection/group_indices in R?

Related 関連記事

  1. 1

    Keep Column which is not in aggregate function in group by statement

  2. 2

    Postgres SQL: column must appear in the GROUP BY clause or be used in an aggregate function

  3. 3

    Rails: must appear in the GROUP BY clause or be used in an aggregate function

  4. 4

    Array of objects intersection

  5. 5

    Mongodb Aggregate Nested Group

  6. 6

    Mongodb Aggregate Nested Group

  7. 7

    Group by / Aggregate Pandas Dataframe

  8. 8

    SQL GROUP BY Aggregate

  9. 9

    Aggregate function, group by clause error when summing difference between two columns

  10. 10

    MySQL aggregate function against different groups which should be computed from an outermost GROUP BY?

  11. 11

    How to group on a column, array aggregate on another and create a single JSON object keyed by the grouped column

  12. 12

    Passing an array of arrays into lodash intersection

  13. 13

    Querying MongoDB collection by array intersection

  14. 14

    R Looping aggregate by group count

  15. 15

    Mongo multiple $lookup and $group in aggregate

  16. 16

    SQL Group By + Aggregate functions with joins

  17. 17

    MongoDB: aggregate and group by splitting the id

  18. 18

    Group by and Aggregate dataset using Python

  19. 19

    Subtract group aggregate values in MongoDB

  20. 20

    Using aggregate in a function

  21. 21

    Problems with SQL sum aggregate function

  22. 22

    MongoDB Aggregate function to C#

  23. 23

    MongoDB Aggregate Array with Two Fields

  24. 24

    MySQL Aggregate Functions without GROUP BY clause

  25. 25

    Spark - group and aggregate only several smallest items

  26. 26

    How to fix aggregate functions are not allowed in GROUP BY?

  27. 27

    Mongodb aggregate query using group-by

  28. 28

    MongoDb aggregate and group by two fields depending on values

  29. 29

    How to renumber result of intersection/group_indices in R?

ホットタグ

アーカイブ