[英]Iterate over all Rows and Columns in DBT - Jinja
我有一張看起來像這樣的表:
ID | 姓名 | 表名 | 聚合類型 | 聚合列 | 條件,獨特 |
---|---|---|---|---|---|
1個 | count_of_courses 課程 | stg_課程 | 數數 | null | null |
2個 | count_of_bundles | stg_bundle | 數數 | null | null |
3個 | 計數產品 | stg_產品 | 數數 | null | null |
4個 | sum_of_gmv | stg_transactions | 和 | 收入_usd | null |
5個 | count_of_course_admins 人數 | stg_用戶 | 數數 | 用戶身份 | is_course_admin |
我的目標是將此表用作元數據以饋送到大型 jinja 循環中,該循環遍歷每個 table_name 並根據上面該表中的元數據執行不同的聚合(計數、求和)。 然后我會將所有表結果合並在一起。
它可能看起來像這樣:
{% set results =
["count_of_courses", "courses", 'count'],
["count_of_bundles", "bundles", 'count'],
["count_of_products", "products", 'count'],
["sum_of_gmv", "stg_transactions", 'sum', 'revenue_usd'],
["count_of_course_admins", "users", 'count', 'user_id', 'is_course_admin is true', 'distinct']
%}
{% for i in results %}
SELECT
user_id,
{{ i[3] }}( --where i[3] refers to the agg_type (COUNT/SUM)
{%- if i[3] == 'sum' -%}
{{- i[4] -}}
{%- elif i[6] == 'distinct' -%}
distinct {{ i[4] }}
{%- else -%}
*
{%- endif -%}
FROM analytics.{{ i[2] }} -- where i[2] refers to the table name
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
尋求有關如何處理瑪瑙表、列表等的幫助。源數據來自 dbt 種子(本質上是一個表),因此我需要一種方法將表轉換為某種列表。
我已經成功地從 dbt 中的 set 運算符手動傳遞了一個列表(如上所示),但我無法將 dbt sedd/model 轉換為該列表格式(也如下所示)。
["count_of_courses", "courses", 'count'],
["count_of_bundles", "bundles", 'count'],
["count_of_products", "products", 'count'],
["sum_of_gmv", "stg_transactions", 'sum', 'revenue_usd'],
["count_of_course_admins", "users", 'count', 'user_id', 'is_course_admin is true', 'distinct']
我很想知道如何最好地將表格(從上面)轉換為這種列表格式,這樣我就可以動態地完成所有這些工作,而不必手動添加到列表中。
謝謝。
這是構建模型的明智方法!
也許最適合您的用例的函數是run_query
(參考此處)。 我無法測試下面的代碼,但它應該是類似的:
{% set fetch_operations_query %}
SELECT * FROM your.seed.table
{% endset %}
{% set results = run_query(fetch_operations_query) %}
{% if execute %}
{% set results_list = results.row.values() %}
{% else %}
{% set results_list = [] %}
{% endif %}
{% for i in result_list %}
SELECT
user_id,
{{ i[3] }}( --where i[3] refers to the agg_type (COUNT/SUM)
{%- if i[3] == 'sum' -%}
{{- i[4] -}}
{%- elif i[6] == 'distinct' -%}
distinct {{ i[4] }}
{%- else -%}
*
{%- endif -%}
FROM analytics.{{ i[2] }} -- where i[2] refers to the table name
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
對於未來的讀者:
我進行了更多的實驗和研究,最終找到了一個通用宏的解決方案,該宏基本上將 SQL 查詢結果存儲在一個列表中。 它看起來像這樣:
{# This macro collects data from a query and inserts it into a list #}
{% macro query_to_list(query) %}
{% set query_to_process %}
{{ query }}
{% endset %}
{% set results = run_query(query_to_process) %}
{% if execute %}
{% set results_list = results.rows %}
{% else %}
{% set results_list = [] %}
{% endif %}
{{ return(results_list) }}
{% endmacro %}
除此之外,我現在可以調用宏並將結果存儲在變量中,對它們進行迭代等。這可能看起來像這樣。 我給出了一個簡化版本,但本質上,您應該能夠解析查詢/變量中的任何值並以神奇的方式使用它們:
將查詢傳遞給宏
{% set query %}
select id, name, table_name
from {{ ref( 'rollups' ) }}
order by id
{% endset %}
遍歷查詢結果
{% for i in query_to_list(query) %}
{{ log(dbt_utils.pretty_log_format(i), info=True) }}
select
user_id,
'{{ i[2] }}' as table_name,
'{{ i[1] }}' as name
from {{ref( [i[2]]|join('') ) }} {# Extracts the name of the table. We use 'join' to convert it to a string #}
{# If there is a condition, we extract that here #}
{% if i[6] %}
where {{ i[6] }}
{% endif %}
group by 1, 2, 3, 4
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.