From 94b30938de3b3ae060ebde3c5e0f70d12a1369e6 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Wed, 23 Nov 2022 11:57:09 -0800 Subject: [PATCH] added more in depth pivot example --- README.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d4b66e1e..8bc754f9 100644 --- a/README.md +++ b/README.md @@ -1034,7 +1034,7 @@ This macro pivots values from rows to columns. {{ dbt_utils.pivot(, ) }} ``` -**Example:** +**Examples:** Input: orders @@ -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