Case Study: Building E-Commerce Data Models with dbt
Susan Sun
Freelance Data Scientist
Jinja statements are enclosed in {% %}:
{% for ... %} ... {% endfor %}
{% for order_status in order_statuses %}
  SUM(
      CASE WHEN status = '{{ order_status }}' 
        THEN order_id 
      END)
{% endfor %}
Repeating SQL:
SELECT 
    user_id,
    SUM(CASE WHEN status = 'Shipped' THEN 1 ELSE 0 END) 
        AS num_orders_Shipped,
    SUM(CASE WHEN status = 'Complete' THEN 1 ELSE 0 END) 
        AS num_orders_Complete,
    SUM(CASE WHEN status = 'Processing' THEN 1 ELSE 0 END) 
        AS num_orders_Processing,        
FROM {{ ref('stg_looker__orders') }}
GROUP BY 1
{% set order_statuses = ['Shipped', 'Complete', 'Processing'] %}
SELECT 
    user_id,
    -- Jinja loop
    {% for order_status in order_statuses %}
        SUM(CASE WHEN status = '{{ order_status }}' THEN 1 ELSE 0 END) 
        -- Parametrized column name
        AS num_orders_{{ order_status }}
    {% endfor %}
FROM {{ ref('stg_looker__orders') }}
GROUP BY 1
dbt compile output:
SELECT 
    user_id,
    SUM(CASE WHEN status = 'Shipped' THEN 1 ELSE 0 END) 
        AS num_orders_Shipped,
    SUM(CASE WHEN status = 'Complete' THEN 1 ELSE 0 END) 
        AS num_orders_Complete,
    SUM(CASE WHEN status = 'Processing' THEN 1 ELSE 0 END) 
        AS num_orders_Processing,        
FROM "dbt"."main"."stg_looker__orders"
GROUP BY 1
A Jinja macro:
/macros folderCOALESCE():
COALESCE(value1, value2, ...)COALESCE(nickname, first_name)An example of a macro function:

Jinja statements are enclosed in {% %}:
{% macro ... %} ... {% endmacro %}
{% macro coalesce_and_round(column_name, decimal_places = 2) %}
      ROUND(COALESCE({{ column_name }}, 0), {{ decimal_places }})
{% endmacro %}
ROUND(COALESCE(sales_amount, 0), 2) AS sales_amount,
ROUND(COALESCE(cost_of_goods_sold, 0), 2) AS cost_of_goods_sold,
coalesce_and_round.sql/macros{{ coalesce_and_round('sales_amount', 2) }} AS sales_amount,
{{ coalesce_and_round('cost_of_goods_sold', 2) }} AS cost_of_goods_sold,
As written:
{% set traffic_source_values = ['Adwords', 'Email', 'Facebook'] %}
{% set browser_values = ['Chrome', 'Firefox', 'Safari', 'IE', 'Other'] %}
SELECT 
    user_id,
    COUNT(DISTINCT session_id) AS num_web_sessions,
...
Compiled:
SELECT 
    user_id,
    COUNT(DISTINCT session_id) AS num_web_sessions,
... 
{%- ... %} strips the whitespace before.{% ... -%} strips the whitespace after.{%- ... -%} strips both.
An example:
{%- set traffic_source_values = ['Adwords', 'Email', 'Facebook'] -%}
{%- set browser_values = ['Chrome', 'Firefox', 'Safari', 'IE', 'Other'] -%}
SELECT 
    user_id,
    COUNT(DISTINCT session_id) AS num_web_sessions,
...
Case Study: Building E-Commerce Data Models with dbt