Skip to content

Instantly share code, notes, and snippets.

@ernestoongaro
Created September 20, 2021 11:46
Show Gist options
  • Save ernestoongaro/15caadc777ff85667afb4897ebd5a5a8 to your computer and use it in GitHub Desktop.
Save ernestoongaro/15caadc777ff85667afb4897ebd5a5a8 to your computer and use it in GitHub Desktop.
/* 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