- カテゴリ:
GROUP BY¶
同じグループごとの式で行をグループ化し、結果のグループの集計関数を計算します。GROUP BY 式は以下のいずれかになります。
列名。
SELECT リスト内の位置を参照する番号。
一般的な式です。
GROUP BY extensions¶
GROUP BYは、強力な集計機能を提供する次の拡張機能をサポートしています。
GROUP BY GROUPING SETS:単一ステートメントで、複数のGROUPBY句をコンピューティングします
GROUP BY ROLLUP:階層データの小計行を生成します
GROUP BY CUBE :ディメンションのすべての組み合わせについて小計行を生成します
これらの拡張機能を通常のGROUP BY列と組み合わせることができます。例:
GROUP BY x, GROUPING SETS(y, z)GROUP BY x, ROLLUP(y, z)GROUP BY x, CUBE(y, z)
拡張結果のNULL値の解釈の詳細については、GROUPING ユーティリティ関数をご参照ください。
構文¶
SELECT ...
FROM ...
[ ... ]
GROUP BY groupItem [ , groupItem [ , ... ] ]
[ ... ]
SELECT ...
FROM ...
[ ... ]
GROUP BY ALL
[ ... ]
条件:
groupItem ::= { <column_alias> | <position> | <expr> }
パラメーター¶
GROUP BY ALL集計関数を使用しない SELECT リスト内のすべての項目をグループ化に使用するように指定します。
例については、 すべての列でグループ化する をご参照ください。
使用上の注意¶
A GROUP BY clause can reference expressions in the projection clause by name or by position. If the GROUP BY clause references by name, each reference is resolved as follows:
If the query contains a database object (for example, a table or view) with a matching column name, the reference is resolved to the column name.
Otherwise, if the projection clause of the SELECT contains an expression alias with a matching name, the reference is resolved to the alias.
例については、 Precedence when a column name and an alias match をご参照ください。
すべての SELECT 項目が集計関数を使用する場合、 GROUP BY ALL を指定することと、 GROUP BY 句なしでステートメントを指定することは同じです。
たとえば、次のステートメントには、集計関数を使用する SELECT 項目のみがあります。
SELECT SUM(amount) FROM mytable GROUP BY ALL;
上記のステートメントは、句によって GROUP を指定しないことと同じです。
SELECT SUM(amount) FROM mytable;
例¶
次のセクションでは、 GROUP BY 句の使用例を示します。
各セクションの例では、 例に対するデータの設定 で設定したデータを使用していることに注意してください。
例に対するデータの設定¶
The examples in this section use a table named sales and a table named product. To create these tables and insert the
data needed for the example, run the following commands:
CREATE TABLE sales (
product_ID INTEGER,
retail_price REAL,
quantity INTEGER,
city VARCHAR,
state VARCHAR);
INSERT INTO sales (product_id, retail_price, quantity, city, state) VALUES
(1, 2.00, 1, 'SF', 'CA'),
(1, 2.00, 2, 'SJ', 'CA'),
(2, 5.00, 4, 'SF', 'CA'),
(2, 5.00, 8, 'SJ', 'CA'),
(2, 5.00, 16, 'Miami', 'FL'),
(2, 5.00, 32, 'Orlando', 'FL'),
(2, 5.00, 64, 'SJ', 'PR');
CREATE TABLE products (
product_ID INTEGER,
wholesale_price REAL);
INSERT INTO products (product_ID, wholesale_price) VALUES (1, 1.00);
INSERT INTO products (product_ID, wholesale_price) VALUES (2, 2.00);
1列でグループ化する¶
This example shows the gross revenue per product, grouped by product_id (that is, the total amount of money received for
each product):
SELECT product_ID, SUM(retail_price * quantity) AS gross_revenue
FROM sales
GROUP BY product_ID;
+------------+---------------+
| PRODUCT_ID | GROSS_REVENUE |
+------------+---------------+
| 1 | 6 |
| 2 | 620 |
+------------+---------------+
この例は前の例を基に構築し、製品ごとの純利益を product_id でグループ化して示しています。
SELECT p.product_ID, SUM((s.retail_price - p.wholesale_price) * s.quantity) AS profit
FROM products AS p, sales AS s
WHERE s.product_ID = p.product_ID
GROUP BY p.product_ID;
+------------+--------+
| PRODUCT_ID | PROFIT |
+------------+--------+
| 1 | 3 |
| 2 | 372 |
+------------+--------+
複数の列でグループ化する¶
次の例は、複数の列でグループ化する方法を示しています。
SELECT state, city, SUM(retail_price * quantity) AS gross_revenue
FROM sales
GROUP BY state, city;
+-------+---------+---------------+
| STATE | CITY | GROSS REVENUE |
+-------+---------+---------------+
| CA | SF | 22 |
| CA | SJ | 44 |
| FL | Miami | 80 |
| FL | Orlando | 160 |
| PR | SJ | 320 |
+-------+---------+---------------+
すべての列でグループ化する¶
次の例は、 複数の列でグループ化する で使用されている例と同じです。
SELECT state, city, SUM(retail_price * quantity) AS gross_revenue
FROM sales
GROUP BY ALL;
+-------+---------+---------------+
| STATE | CITY | GROSS REVENUE |
+-------+---------+---------------+
| CA | SF | 22 |
| CA | SJ | 44 |
| FL | Miami | 80 |
| FL | Orlando | 160 |
| PR | SJ | 320 |
+-------+---------+---------------+
Precedence when a column name and an alias match¶
It is possible (but usually not recommended) to create a query that contains an alias that matches a column name:
SELECT x, some_expression AS x
FROM ...
If a clause contains a name that matches both a column name and an alias, then the clause uses the column name. The following example demonstrates this behavior using a GROUP BY clause:
テーブルを作成して行を挿入します。
CREATE TABLE employees (salary FLOAT, state VARCHAR, employment_state VARCHAR);
INSERT INTO employees (salary, state, employment_state) VALUES
(60000, 'California', 'Active'),
(70000, 'California', 'On leave'),
(80000, 'Oregon', 'Active');
The following query returns the sum of the salaries of the employees who are active and the sum of the salaries of the employees who are on leave:
SELECT SUM(salary), ANY_VALUE(employment_state)
FROM employees
GROUP BY employment_state;
+-------------+-----------------------------+
| SUM(SALARY) | ANY_VALUE(EMPLOYMENT_STATE) |
|-------------+-----------------------------|
| 140000 | Active |
| 70000 | On leave |
+-------------+-----------------------------+
The next query uses the alias state, which matches the name of a column of the table in the query. When state is used in
the GROUP BY clause, Snowflake interprets it as a reference to the column name, not the alias. This query therefore returns the sum of
the salaries of the employees in the state of California and the sum of the salaries of the employees in the state of Oregon,
yet displays employment_state information, such as Active, rather than the names of states or provinces:
SELECT SUM(salary), ANY_VALUE(employment_state) AS state
FROM employees
GROUP BY state;
+-------------+--------+
| SUM(SALARY) | STATE |
|-------------+--------|
| 130000 | Active |
| 80000 | Active |
+-------------+--------+