Skip to content

Commit

Permalink
added more in depth pivot example (dbt-labs#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd authored Nov 25, 2022
1 parent 88afd9f commit 6d76641
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ This macro pivots values from rows to columns.
{{ dbt_utils.pivot(<column>, <list of values>) }}
```

**Example:**
**Examples:**

Input: orders

Expand All @@ -1061,6 +1061,36 @@ This macro pivots values from rows to columns.
| S | 2 | 1 |
| M | 1 | 0 |

Input: orders

| size | color | quantity |
|------|-------|----------|
| S | red | 1 |
| S | blue | 2 |
| S | red | 4 |
| M | red | 8 |

select
size,
{{ dbt_utils.pivot(
'color',
dbt_utils.get_column_values(ref('orders'), 'color'),
agg='sum',
then_value='quantity',
prefix='pre_',
suffix='_post'
) }}
from {{ ref('orders') }}
group by size

Output:

| size | pre_red_post | pre_blue_post |
|------|--------------|---------------|
| S | 5 | 2 |
| M | 8 | 0 |


**Args:**

- `column`: Column name, required
Expand Down

0 comments on commit 6d76641

Please sign in to comment.