SQL
Examples
This example uses a single value expression, a single column expression, and a single row expression:  
```sql
SELECT *
FROM cities
PIVOT (
sum(population)
FOR
year IN (2000, 2010, 2020)
GROUP BY country
);
```  
<div class="narrow_table"></div>  
| country | 2000 | 2010 | 2020 |
|---------|-----:|-----:|-----:|
| NL      | 1005 | 1065 | 1158 |
| US      | 8579 | 8783 | 9510 |  
This example is somewhat contrived, but serves as an example of using multiple value expressions and multiple columns in the `FOR` clause.  
```sql
SELECT *
FROM cities
PIVOT (
sum(population) AS total,
count(population) AS count
FOR
year IN (2000, 2010)
country IN ('NL', 'US')
);
```  
|     name      | 2000_NL_total | 2000_NL_count | 2000_US_total | 2000_US_count | 2010_NL_total | 2010_NL_count | 2010_US_total | 2010_US_count |
|--|-:|-:|-:|-:|-:|-:|-:|-:|
| Amsterdam     | 1005          | 1             | NULL          | 0             | 1065          | 1             | NULL          | 0             |
| Seattle       | NULL          | 0             | 564           | 1             | NULL          | 0             | 608           | 1             |
| New York City | NULL          | 0             | 8015          | 1             | NULL          | 0             | 8175          | 1             |  
#### SQL Standard `PIVOT` Full Syntax Diagram {#docs:sql:statements:pivot::sql-standard-pivot-full-syntax-diagram}  
Below is the full syntax diagram of the SQL Standard version of the `PIVOT` statement.  
<div id="rrdiagram2"></div>