Nghiên cứu tình huống: Xây dựng mô hình dữ liệu E-Commerce với dbt
Susan Sun
Freelance Data Scientist
Các statement Jinja được bao trong {% %}:
{% for ... %} ... {% endfor %}
{% for order_status in order_statuses %}
SUM(
CASE WHEN status = '{{ order_status }}'
THEN order_id
END)
{% endfor %}
SQL lặp lại:
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,
-- Vòng lặp Jinja
{% for order_status in order_statuses %}
SUM(CASE WHEN status = '{{ order_status }}' THEN 1 ELSE 0 END)
-- Tên cột tham số hóa
AS num_orders_{{ order_status }}
{% endfor %}
FROM {{ ref('stg_looker__orders') }}
GROUP BY 1
Kết quả dbt compile:
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
Một macro Jinja:
/macrosCOALESCE():
COALESCE(value1, value2, ...)COALESCE(nickname, first_name)Ví dụ một hàm macro:

Các statement Jinja được bao trong {% %}:
{% 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,
Như đã viết:
{% 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,
...
Sau biên dịch:
SELECT
user_id,
COUNT(DISTINCT session_id) AS num_web_sessions,
...
{%- ... %} loại bỏ khoảng trắng trước.{% ... -%} loại bỏ khoảng trắng sau.{%- ... -%} loại bỏ cả hai.
Ví dụ:
{%- 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,
...
Nghiên cứu tình huống: Xây dựng mô hình dữ liệu E-Commerce với dbt