huaxingao commented on a change in pull request #29056: URL: https://github.com/apache/spark/pull/29056#discussion_r455452971
########## File path: docs/sql-ref-syntax-qry-select-pivot.md ########## @@ -0,0 +1,98 @@ +--- +layout: global +title: PIVOT Clause +displayTitle: PIVOT Clause +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +### Description + +`PIVOT` clause is used for data perspective, we can get the aggregated values based on specific column value. + +### Syntax + +```sql +PIVOT ( { group_expression [ AS group_expression_alias ] } [ , ... ] FOR column_list IN ( expression_list ) ) +``` + +### Parameters + +* **group_expression** + + We will get specific aggregated results by the `aggregate_expression`, such as `SUM(a)` or `COUNT(DISTINCT b)`. + +* **group_expression_alias** + + It is the alias for `group_expression`, which is optional, we can use it in `SELECT` clause. + +* **column_list** + + It contains columns in the `FROM` clause, which specific the columns we want to replaced with new columns, we can use brackets to surround the columns, such as `( c1, c2 )`. + +* **expression_list** + + It specifics new columns , which used to match values in `column_List` as the aggregating condition, we can also add alias for them. + +### Examples + +```sql +CREATE TABLE person (id INT, name STRING, age INT, class INT, address STRING); +INSERT INTO person VALUES + (100, 'John', 30, 1, 'Street 1'), + (200, 'Mary', NULL, 1, 'Street 2'), + (300, 'Mike', 80, 3, 'Street 3'), + (400, 'Dan', 50, 4, 'Street 4'); + +SELECT * FROM person +PIVOT ( Review comment: I think we always indent 4 spaces if the SQL statement is more than one line. ########## File path: docs/sql-ref-syntax-qry-select-pivot.md ########## @@ -0,0 +1,98 @@ +--- +layout: global +title: PIVOT Clause +displayTitle: PIVOT Clause +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +### Description + +`PIVOT` clause is used for data perspective, we can get the aggregated values based on specific column value. + +### Syntax + +```sql +PIVOT ( { group_expression [ AS group_expression_alias ] } [ , ... ] FOR column_list IN ( expression_list ) ) +``` + +### Parameters + +* **group_expression** + + We will get specific aggregated results by the `aggregate_expression`, such as `SUM(a)` or `COUNT(DISTINCT b)`. + +* **group_expression_alias** + + It is the alias for `group_expression`, which is optional, we can use it in `SELECT` clause. + +* **column_list** + + It contains columns in the `FROM` clause, which specific the columns we want to replaced with new columns, we can use brackets to surround the columns, such as `( c1, c2 )`. + +* **expression_list** + + It specifics new columns , which used to match values in `column_List` as the aggregating condition, we can also add alias for them. + +### Examples + +```sql +CREATE TABLE person (id INT, name STRING, age INT, class INT, address STRING); +INSERT INTO person VALUES + (100, 'John', 30, 1, 'Street 1'), + (200, 'Mary', NULL, 1, 'Street 2'), + (300, 'Mike', 80, 3, 'Street 3'), + (400, 'Dan', 50, 4, 'Street 4'); + +SELECT * FROM person +PIVOT ( + SUM(age) AS a, AVG(class) AS c + FOR name IN ('John' AS john, 'Mike' AS mike) +); ++------+-----------+---------+---------+---------+---------+ +| id | address | john_a | john_c | mike_a | mike_c | ++------+-----------+---------+---------+---------+---------+ +| 200 | Street 2 | NULL | NULL | NULL | NULL | +| 100 | Street 1 | 30 | 1.0 | NULL | NULL | +| 300 | Street 3 | NULL | NULL | 80 | 3.0 | +| 400 | Street 4 | NULL | NULL | NULL | NULL | ++------+-----------+---------+---------+---------+---------+ + +SELECT * FROM person +PIVOT ( + SUM(age) AS a, AVG(class) AS c + FOR (name, age) IN (('John', 30) AS c1, ('Mike', 40) AS c2) +); ++------+-----------+-------+-------+-------+-------+ +| id | address | c1_a | c1_c | c2_a | c2_c | ++------+-----------+-------+-------+-------+-------+ +| 200 | Street 2 | NULL | NULL | NULL | NULL | +| 100 | Street 1 | 30 | 1.0 | NULL | NULL | +| 300 | Street 3 | NULL | NULL | NULL | NULL | +| 400 | Street 4 | NULL | NULL | NULL | NULL | ++------+-----------+-------+-------+-------+-------+--+ +``` + +### Related Statements + +* [SELECT Main](sql-ref-syntax-qry-select.html) +* [WHERE Clause](sql-ref-syntax-qry-select-where.html) +* [GROUP BY Clause](sql-ref-syntax-qry-select-groupby.html) +* [HAVING Clause](sql-ref-syntax-qry-select-having.html) +* [ORDER BY Clause](sql-ref-syntax-qry-select-orderby.html) +* [SORT BY Clause](sql-ref-syntax-qry-select-sortby.html) +* [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html) +* [LIMIT Clause](sql-ref-syntax-qry-select-limit.html) Review comment: Could you also link to the new doc you added? ########## File path: docs/sql-ref-syntax-qry-select-pivot.md ########## @@ -0,0 +1,98 @@ +--- +layout: global +title: PIVOT Clause +displayTitle: PIVOT Clause +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +### Description + +`PIVOT` clause is used for data perspective, we can get the aggregated values based on specific column value. + +### Syntax + +```sql +PIVOT ( { group_expression [ AS group_expression_alias ] } [ , ... ] FOR column_list IN ( expression_list ) ) +``` + +### Parameters + +* **group_expression** + + We will get specific aggregated results by the `aggregate_expression`, such as `SUM(a)` or `COUNT(DISTINCT b)`. + +* **group_expression_alias** + + It is the alias for `group_expression`, which is optional, we can use it in `SELECT` clause. + +* **column_list** + + It contains columns in the `FROM` clause, which specific the columns we want to replaced with new columns, we can use brackets to surround the columns, such as `( c1, c2 )`. + +* **expression_list** + + It specifics new columns , which used to match values in `column_List` as the aggregating condition, we can also add alias for them. + +### Examples + +```sql +CREATE TABLE person (id INT, name STRING, age INT, class INT, address STRING); +INSERT INTO person VALUES + (100, 'John', 30, 1, 'Street 1'), + (200, 'Mary', NULL, 1, 'Street 2'), + (300, 'Mike', 80, 3, 'Street 3'), + (400, 'Dan', 50, 4, 'Street 4'); + +SELECT * FROM person +PIVOT ( + SUM(age) AS a, AVG(class) AS c + FOR name IN ('John' AS john, 'Mike' AS mike) +); ++------+-----------+---------+---------+---------+---------+ +| id | address | john_a | john_c | mike_a | mike_c | ++------+-----------+---------+---------+---------+---------+ +| 200 | Street 2 | NULL | NULL | NULL | NULL | +| 100 | Street 1 | 30 | 1.0 | NULL | NULL | +| 300 | Street 3 | NULL | NULL | 80 | 3.0 | +| 400 | Street 4 | NULL | NULL | NULL | NULL | ++------+-----------+---------+---------+---------+---------+ + +SELECT * FROM person +PIVOT ( + SUM(age) AS a, AVG(class) AS c + FOR (name, age) IN (('John', 30) AS c1, ('Mike', 40) AS c2) +); ++------+-----------+-------+-------+-------+-------+ +| id | address | c1_a | c1_c | c2_a | c2_c | ++------+-----------+-------+-------+-------+-------+ +| 200 | Street 2 | NULL | NULL | NULL | NULL | +| 100 | Street 1 | 30 | 1.0 | NULL | NULL | +| 300 | Street 3 | NULL | NULL | NULL | NULL | +| 400 | Street 4 | NULL | NULL | NULL | NULL | ++------+-----------+-------+-------+-------+-------+--+ Review comment: remove --+? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
