Snowpark Migration Accelerator: グループ別

説明

The GROUP BY clause groups rows based on specified expressions and calculates aggregate functions for each group. Databricks SQL provides advanced grouping options through GROUPING SETS, CUBE, and ROLLUP clauses, which allow multiple aggregations on the same dataset. You can combine regular grouping expressions with these advanced options in the GROUP BY clause, and nest them within GROUPING SETS. (Databricks SQL Language Reference GROUP BY)

指定した列で同じ値を共有する行をグループ化し、各グループについて集計関数(SUM、 COUNT、 AVG など)を計算します。GROUP BY 句には以下を含めることができます。

  • 列の名前

  • A number that refers to a position in the SELECT list

  • 任意の有効な式

拡張機能:

GROUP BY CUBE, GROUP BY GROUPING SETS, and GROUP BY ROLLUP

Snowflake SQL Language Reference GROUP BY

構文

GROUP BY ALL

GROUP BY group_expression [, ...] [ WITH ROLLUP | WITH CUBE ]

GROUP BY { group_expression | { ROLLUP | CUBE | GROUPING SETS } ( grouping_set [, ...] ) } [, ...]

grouping_set
   { expression |
     ( [ expression [, ...] ] ) }
SELECT ...
  FROM ...
  [ ... ]
  GROUP BY groupItem [ , groupItem [ , ... ] ]
  [ ... ]

SELECT ...
  FROM ...
  [ ... ]
  GROUP BY ALL
  [ ... ]
groupItem ::= { <column_alias> | <position> | <expr> }

SELECT ...
FROM ...
[ ... ]
GROUP BY CUBE ( groupCube [ , groupCube [ , ... ] ] )
[ ... ]

groupCube ::= { <column_alias> | <position> | <expr> }

SELECT ...
FROM ...
[ ... ]
GROUP BY GROUPING SETS ( groupSet [ , groupSet [ , ... ] ] )
[ ... ]

groupSet ::= { <column_alias> | <position> | <expr> }

SELECT ...
FROM ...
[ ... ]
GROUP BY ROLLUP ( groupRollup [ , groupRollup [ , ... ] ] )
[ ... ]

groupRollup ::= { <column_alias> | <position> | <expr> }

サンプルソースパターン

セットアップデータ

Databricks

CREATE TEMP VIEW dealer (id, city, car_model, quantity) AS
VALUES (100, 'Fremont', 'Honda Civic', 10),
       (100, 'Fremont', 'Honda Accord', 15),
       (100, 'Fremont', 'Honda CRV', 7),
       (200, 'Dublin', 'Honda Civic', 20),
       (200, 'Dublin', 'Honda Accord', 10),
       (200, 'Dublin', 'Honda CRV', 3),
       (300, 'San Jose', 'Honda Civic', 5),
       (300, 'San Jose', 'Honda Accord', 8);

Snowflake

CREATE TEMP TABLE dealer (id INT, city STRING, car_model STRING, quantity INT);
INSERT INTO dealer VALUES
        (100, 'Fremont', 'Honda Civic', 10),
        (100, 'Fremont', 'Honda Accord', 15),
        (100, 'Fremont', 'Honda CRV', 7),
        (200, 'Dublin', 'Honda Civic', 20),
        (200, 'Dublin', 'Honda Accord', 10),
        (200, 'Dublin', 'Honda CRV', 3),
        (300, 'San Jose', 'Honda Civic', 5),
        (300, 'San Jose', 'Honda Accord', 8);

パターンコード

Databricks

-- 1. Sum of quantity per dealership. Group by `id`.
SELECT id, sum(quantity) FROM dealer GROUP BY id ORDER BY id;

-- 2. Use column position in GROUP by clause.
SELECT id, sum(quantity) FROM dealer GROUP BY 1 ORDER BY 1;

-- 3. Multiple aggregations.
-- 3.1. Sum of quantity per dealership.
-- 3.2. Max quantity per dealership.
SELECT id, sum(quantity) AS sum, max(quantity) AS max
    FROM dealer GROUP BY id ORDER BY id;

-- 4. Count the number of distinct dealers in cities per car_model.
SELECT car_model, count(DISTINCT city) AS count FROM dealer GROUP BY car_model;

-- 5. Count the number of distinct dealers in cities per car_model, using GROUP BY ALL
SELECT car_model, count(DISTINCT city) AS count FROM dealer GROUP BY ALL;

-- 6. Sum of only 'Honda Civic' and 'Honda CRV' quantities per dealership.
SELECT id,
         sum(quantity) FILTER (WHERE car_model IN ('Honda Civic', 'Honda CRV')) AS `sum(quantity)`
    FROM dealer
    GROUP BY id ORDER BY id;

-- 7. Aggregations using multiple sets of grouping columns in a single statement.
-- Following performs aggregations based on four sets of grouping columns.
-- 7.1. city, car_model
-- 7.2. city
-- 7.3. car_model
-- 7.4. Empty grouping set. Returns quantities for all city and car models.
SELECT city, car_model, sum(quantity) AS sum
    FROM dealer
    GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
    ORDER BY city;

-- 8.Group by processing with `ROLLUP` clause.
-- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), ())
SELECT city, car_model, sum(quantity) AS sum
    FROM dealer
    GROUP BY city, car_model WITH ROLLUP
    ORDER BY city, car_model;

-- 9. Group by processing with `CUBE` clause.
-- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
SELECT city, car_model, sum(quantity) AS sum
    FROM dealer
    GROUP BY city, car_model WITH CUBE
    ORDER BY city, car_model;

id

sum(quantity)

100

32

200

33

300

13

id

sum(quantity)

100

32

200

33

300

13

id

sum

max

100

32

15

200

33

20

300

13

8

car_model

count

ホンダ シビック

3

ホンダ CRV

2

ホンダ アコード

3

car_model

count

ホンダ シビック

3

ホンダ CRV

2

ホンダ アコード

3

id

sum(quantity)

100

17

200

23

300

5

city

car_model

sum

NULL

ホンダ シビック

35

NULL

ホンダ アコード

33

NULL

NULL

78

NULL

ホンダ CRV

10

ダブリン

ホンダ シビック

20

ダブリン

NULL

33

ダブリン

ホンダ CRV

3

ダブリン

ホンダ アコード

10

フリーモント

ホンダ アコード

15

フリーモント

ホンダ シビック

10

フリーモント

NULL

32

フリーモント

ホンダ CRV

7

サンノゼ

ホンダ アコード

8

サンノゼ

NULL

13

サンノゼ

ホンダ シビック

5

city

car_model

sum

NULL

NULL

78

ダブリン

NULL

33

ダブリン

ホンダ アコード

10

ダブリン

ホンダ CRV

3

ダブリン

ホンダ シビック

20

フリーモント

NULL

32

フリーモント

ホンダ アコード

15

フリーモント

ホンダ CRV

7

フリーモント

ホンダ シビック

10

サンノゼ

NULL

13

サンノゼ

ホンダ アコード

8

サンノゼ

ホンダ シビック

5

city

car_model

sum

NULL

NULL

78

NULL

ホンダ アコード

33

NULL

ホンダ CRV

10

NULL

ホンダ シビック

35

ダブリン

NULL

33

ダブリン

ホンダ アコード

10

ダブリン

ホンダ CRV

3

ダブリン

ホンダ シビック

20

フリーモント

NULL

32

フリーモント

ホンダ アコード

15

フリーモント

ホンダ CRV

7

フリーモント

ホンダ シビック

10

サンノゼ

NULL

13

サンノゼ

ホンダ アコード

8

サンノゼ

ホンダ シビック

5

Snowflake

-- 1. Sum of quantity per dealership. Group by `id`.
SELECT id, sum(quantity) FROM dealer GROUP BY id ORDER BY id;

-- 2. Use column position in GROUP by clause.
SELECT id, sum(quantity) FROM dealer GROUP BY 1 ORDER BY 1;

-- 3. Multiple aggregations.
-- 3.1. Sum of quantity per dealership.
-- 3.2. Max quantity per dealership.
SELECT id, sum(quantity) AS sum, max(quantity) AS max
    FROM dealer GROUP BY id ORDER BY id;

-- 4. Count the number of distinct dealers in cities per car_model.
SELECT car_model, count(DISTINCT city) AS count FROM dealer GROUP BY car_model;

-- 5. Count the number of distinct dealers in cities per car_model, using GROUP BY ALL
SELECT car_model, count(DISTINCT city) AS count FROM dealer GROUP BY ALL;

-- 6. Sum of only 'Honda Civic' and 'Honda CRV' quantities per dealership.
SELECT
    id,
    SUM(CASE WHEN car_model='Honda Civic' OR car_model='Honda CRV' THEN quantity ELSE NULL END) AS `sum(quantity)`
    FROM dealer
    GROUP BY id ORDER BY id;

-- 7. Aggregations using multiple sets of grouping columns in a single statement.
-- Following performs aggregations based on four sets of grouping columns.
-- 7.1. city, car_model
-- 7.2. city
-- 7.3. car_model
-- 7.4. Empty grouping set. Returns quantities for all city and car models.
SELECT city, car_model, sum(quantity) AS sum
    FROM dealer
    GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
    ORDER BY city NULLS FIRST;


-- 8. Group by processing with `ROLLUP` clause.
-- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), ())
SELECT city, car_model, sum(quantity) AS sum
    FROM dealer
    GROUP BY ROLLUP (city, car_model)
    ORDER BY city NULLS FIRST, car_model NULLS FIRST;

-- 9. Group by processing with `CUBE` clause.
-- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
SELECT city, car_model, sum(quantity) AS sum
    FROM dealer
    GROUP BY CUBE (city, car_model)
    ORDER BY city NULLS FIRST, car_model NULLS FIRST;

id

sum(quantity)

100

32

200

33

300

13

id

sum(quantity)

100

32

200

33

300

13

id

sum

max

100

32

15

200

33

20

300

13

8

car_model

count

ホンダ シビック

3

ホンダ CRV

2

ホンダ アコード

3

car_model

count

ホンダ シビック

3

ホンダ CRV

2

ホンダ アコード

3

id

sum(quantity)

100

17

200

23

300

5

city

car_model

sum

NULL

ホンダ シビック

35

NULL

ホンダ アコード

33

NULL

NULL

78

NULL

ホンダ CRV

10

ダブリン

ホンダ シビック

20

ダブリン

NULL

33

ダブリン

ホンダ CRV

3

ダブリン

ホンダ アコード

10

フリーモント

ホンダ アコード

15

フリーモント

ホンダ シビック

10

フリーモント

NULL

32

フリーモント

ホンダ CRV

7

サンノゼ

ホンダ アコード

8

サンノゼ

NULL

13

サンノゼ

ホンダ シビック

5

city

car_model

sum

NULL

NULL

78

ダブリン

NULL

33

ダブリン

ホンダ アコード

10

ダブリン

ホンダ CRV

3

ダブリン

ホンダ シビック

20

フリーモント

NULL

32

フリーモント

ホンダ アコード

15

フリーモント

ホンダ CRV

7

フリーモント

ホンダ シビック

10

サンノゼ

NULL

13

サンノゼ

ホンダ アコード

8

サンノゼ

ホンダ シビック

5

city

car_model

sum

NULL

NULL

78

NULL

ホンダ アコード

33

NULL

ホンダ CRV

10

NULL

ホンダ シビック

35

ダブリン

NULL

33

ダブリン

ホンダ アコード

10

ダブリン

ホンダ CRV

3

ダブリン

ホンダ シビック

20

フリーモント

NULL

32

フリーモント

ホンダ アコード

15

フリーモント

ホンダ CRV

7

フリーモント

ホンダ シビック

10

サンノゼ

NULL

13

サンノゼ

ホンダ アコード

8

サンノゼ

ホンダ シビック

5

既知の問題

問題は見つかりませんでした