Snowpark Migration Accelerator: グループ別

説明

GROUPBY 句は、指定された式に基づいて行をグループ化し、各グループの集計関数を計算します。Databricks SQL は、 GROUPING SETSCUBEROLLUP 句によって高度なグループ化オプションを提供し、同じデータセットに対して複数の集計を可能にします。GROUP BY 句で正規グループ化表現とこれらの高度なオプションを組み合わせ、 GROUPING SETS の中にネストすることができます。(Databricks SQL 言語参照 GROUP BY)。

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

  • 列の名前

  • SELECT リスト内の位置を参照する数値。

  • 任意の有効な式

拡張機能:

GROUP BY CUBEGROUP BY GROUPING SETS、および GROUP BY ROLLUP

Snowflake SQL 言語参照 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 [, ...] ] ) }
Copy
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> }
Copy

サンプルソースパターン

セットアップデータ

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

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

パターンコード

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

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

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

既知の問題

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