Created
September 20, 2021 11:46
-
-
Save ernestoongaro/15caadc777ff85667afb4897ebd5a5a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Create a pivot table with dynamic columns based on the ship modes that are in the system */ | |
{%- call statement('result', fetch_result=True) -%} | |
{# this pulls the unique ship modes from the fct_order_items table #} | |
select ship_mode from {{ ref('fct_order_items') }} group by 1 | |
{%- endcall %} | |
{% set ship_modes = load_result('result').table.columns[0].values() %} | |
select | |
date_part('year', order_date) as order_year, | |
{# Loop over ship_modes array from above, and sum based on whether the record matches the ship mode #} | |
{%- for ship_mode in ship_modes -%} | |
sum(case when ship_mode = '{{ship_mode}}' then gross_item_sales_amount end) as "{{ship_mode|replace(' ', '_')}}_amount" | |
{%- if not loop.last -%},{% endif %} | |
{% endfor %} | |
from {{ ref('fct_order_items') }} | |
group by 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment