Created
September 27, 2021 11:24
-
-
Save ernestoongaro/df5f6bd1d3fef89479565bfde1e6fce6 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
=== | |
Compiled SQL: | |
select | |
EXTRACT(YEAR from ship_date), | |
sum(case when ship_mode = 'FOB' then gross_item_sales_amount end) as FOB_A_amount, | |
sum(case when ship_mode = 'TRUCK' then gross_item_sales_amount end) as TRUCK_N_amount, | |
sum(case when ship_mode = 'MAIL' then gross_item_sales_amount end) as MAIL_N_amount, | |
sum(case when ship_mode = 'SHIP' then gross_item_sales_amount end) as SHIP_R_amount, | |
sum(case when ship_mode = 'AIR' then gross_item_sales_amount end) as AIR_R_amount, | |
sum(case when ship_mode = 'RAIL' then gross_item_sales_amount end) as RAIL_A_amount, | |
sum(case when ship_mode = 'RAIL' then gross_item_sales_amount end) as RAIL_R_amount, | |
sum(case when ship_mode = 'SHIP' then gross_item_sales_amount end) as SHIP_N_amount, | |
sum(case when ship_mode = 'MAIL' then gross_item_sales_amount end) as MAIL_A_amount, | |
sum(case when ship_mode = 'TRUCK' then gross_item_sales_amount end) as TRUCK_R_amount, | |
sum(case when ship_mode = 'SHIP' then gross_item_sales_amount end) as SHIP_A_amount, | |
sum(case when ship_mode = 'RAIL' then gross_item_sales_amount end) as RAIL_N_amount, | |
sum(case when ship_mode = 'AIR' then gross_item_sales_amount end) as AIR_N_amount, | |
sum(case when ship_mode = 'FOB' then gross_item_sales_amount end) as FOB_N_amount, | |
sum(case when ship_mode = 'FOB' then gross_item_sales_amount end) as FOB_R_amount, | |
sum(case when ship_mode = 'MAIL' then gross_item_sales_amount end) as MAIL_R_amount, | |
sum(case when ship_mode = 'TRUCK' then gross_item_sales_amount end) as TRUCK_A_amount, | |
sum(case when ship_mode = 'AIR' then gross_item_sales_amount end) as AIR_A_amount | |
from `sales-demo-project-314714`.`dbt_eongaro`.`fct_order_items` | |
group by 1 | |
limit 500 | |
=== | |
/* Create a pivot table with dynamic columns based on the ship modes that are in the system */ | |
{%- call statement('result', fetch_result=True) -%} | |
{# get ship modes and return flags from the fct_order_items table - stores them as a python list of tuples [('N', 'FOB'), ('A', 'TRUCK') #} | |
select return_flag,ship_mode from {{ ref('fct_order_items') }} | |
where ship_mode != 'REG AIR' | |
group by 1,2 | |
{%- endcall %} | |
{% set ship_modes = load_result('result').data %} | |
select | |
EXTRACT(YEAR from ship_date), | |
{# Loop over ship_modes list and access the tuple items #} | |
{%- for ship_mode in ship_modes -%} | |
sum(case when ship_mode = '{{ship_mode[1]}}' then gross_item_sales_amount end) as {{ship_mode[1]|replace(' ', '_')}}_{{ship_mode[0]}}_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