

# `PIVOT`

`PIVOT` turns rows into columns. For each value in its `IN` list, it creates a column that holds an aggregate, such as `sum(amount)`, of the rows that have that value. The other columns of the source become grouping columns, and the result has one row for each combination of their values. To group by fewer columns, add `GROUP BY`, or pivot a subquery that selects only the columns you need.

You can write `PIVOT` in two forms. The short form, `PIVOT` `source` `ON` `column` `IN (...)` `USING` `aggregate`, is a query of its own that you can also use as a subquery. The SQL standard form follows a source in the `FROM` clause: `source` `PIVOT (``aggregate` `FOR` `column` `IN (...))`.

Each new column is named after its value. To choose the name, give the value an alias in the `IN` list, as in `IN ('east' AS east_revenue)`. With more than one aggregate, give each aggregate an alias. The column names then join the value and the alias with an underscore, such as `east_total`.

## Syntax
<a name="athena-duckdb-statement-select-pivot-syntax"></a>

```sql
PIVOT {{source}} ON {{column}} IN ( {{value}} [ AS {{name}} ]
    [, ...] ) USING {{aggregate}} [ AS {{alias}} ] [, ...]
    [ GROUP BY {{column}} [, ...] ]
{{source}} PIVOT ( {{aggregate}} [ AS {{alias}} ]
    [, ...] FOR {{column}} IN ( {{value}} [ AS {{name}} ]
    [, ...] ) [ GROUP BY {{column}} [, ...] ] )
```

## Examples
<a name="athena-duckdb-statement-select-pivot-examples"></a>

For a description of the sample tables that the examples on this page use, see [SELECT statement in DuckDB](duckdb-statement-select.md).

The following SQL query totals the amounts of the `east` and `west` regions in a column for each, with one row for each date. A column is `NULL` for a date that has no order in that region, or only an order with a `NULL` amount.

```sql
SELECT order_date, east_revenue, west_revenue
FROM (
  PIVOT sales_db.sales_parquet
  ON region IN (
      'east' AS east_revenue, 'west' AS west_revenue
  )
  USING sum(amount)
  GROUP BY order_date
)
ORDER BY order_date
```

The query returns the following results.


| order\_date | east\_revenue | west\_revenue | 
| --- | --- | --- | 
| `2026-10-01` | `120.50` | `75.00` | 
| `2026-10-02` | `42.25` | `NULL` | 
| `2026-10-03` | `NULL` | `18.75` | 
| `2026-10-04` | `NULL` | `NULL` | 

The following SQL query uses the SQL standard form on a subquery that selects only `region` and `amount`, so the result is one row.

```sql
SELECT east, north, west
FROM (SELECT region, amount FROM sales_db.sales_parquet)
  PIVOT (
      sum(amount) FOR region IN (
          'east', 'north', 'west'
      )
  )
```

The query returns the following result.


| east | north | west | 
| --- | --- | --- | 
| `162.75` | `310.00` | `93.75` | 

The following SQL query computes two aggregates for each region, and shows how the columns are named.

```sql
SELECT east_total, east_orders, west_total, west_orders
FROM (
  PIVOT (
      SELECT region, amount FROM sales_db.sales_parquet
  )
  ON region IN ('east', 'west')
  USING sum(amount) AS total, count(*) AS orders
)
```

The query returns the following result.


| east\_total | east\_orders | west\_total | west\_orders | 
| --- | --- | --- | --- | 
| `162.75` | `3` | `93.75` | `2` | 

## Considerations
<a name="athena-duckdb-statement-select-pivot-considerations"></a>

List the values to pivot on with `IN (...)`. Athena does not support a `PIVOT` without an `IN` list, which would take its columns from the values in the data. The `IN` list also cannot be a subquery, such as `IN (SELECT DISTINCT region FROM sales_db.sales_parquet)`.

Rows whose value is not in the `IN` list are left out of the new columns.