for 循环中的 DBT 引用

发布于 2025-01-16 11:56:59 字数 1147 浏览 3 评论 0原文

我的 dbt 项目中有一个子查询列表 [“base”、“table_1”、“table_2”、“table_3”...]。

我想将它们加入到一个共同的列 id 中。

我想通过使用 for 循环宏来避免重复。我的问题是,当我尝试在 {{ref}} 中引用子查询名称 {{sub}} 时,出现语法错误。

这是我正在尝试的代码。

{% set subs = ["table_1", "table_2", "table_3"] %}


SELECT
{% for sub in subs %}
    {{sub}}.* EXCEPT (id),
{% endfor %}
base.*
FROM {{ref('base')}} as base
{% for sub in subs %}
    LEFT JOIN {{ref({{sub}})}} as {{sub}}
    ON {{sub}}.id = base.id
{% endfor %}

我收到语法错误

expected token ':', got '}'

如果我更改为

LEFT JOIN {{ref("'"{{sub}}"'")}} as {{sub}}

我收到此错误

expected token ',', got '{'

最后使用

LEFT JOIN {{ref("{{sub}}") }} 作为{{sub}}

我得到

模型依赖于一个名为“{{sub}}”的节点,但未找到

以下是我读过的一些页面,但找不到解决方案

在 Jinja 宏中使用字符串作为参数在dbt

https://docs.getdbt.com/reference/dbt -jinja-functions/ref

I have a list of subqueries in my dbt project ["base","table_1", "table_2", "table_3"...].

I would like to join them all on a common column, id.

I would like to avoid repetition by doing this using for loop macro. My problem is, I get syntax errors when I try to reference the subquery name {{sub}} within {{ref}}.

Here is the code I was trying.

{% set subs = ["table_1", "table_2", "table_3"] %}


SELECT
{% for sub in subs %}
    {{sub}}.* EXCEPT (id),
{% endfor %}
base.*
FROM {{ref('base')}} as base
{% for sub in subs %}
    LEFT JOIN {{ref({{sub}})}} as {{sub}}
    ON {{sub}}.id = base.id
{% endfor %}

I get a syntax error

expected token ':', got '}'

And if I change to

LEFT JOIN {{ref("'"{{sub}}"'")}} as {{sub}}

I get this error

expected token ',', got '{'

Finally with

LEFT JOIN {{ref("{{sub}}")}} as {{sub}}

I get

Model depends on a node named '{{sub}}' which was not found

Here are some pages I read but couldn't see a solution

Using a string as an argument in a Jinja macro in dbt

https://docs.getdbt.com/reference/dbt-jinja-functions/ref

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2025-01-23 11:57:00

当您使用 ref() 时,您已经位于表达式子句 ({{..}}) 内,因此您不需要添加另一个表达式子句来引用你的潜艇。

{% set subs = ["table_1", "table_2", "table_3"] %}


SELECT
{% for sub in subs %}
    {{sub}}.* EXCEPT (id),
{% endfor %}
base.*
FROM {{ref('base')}} as base
{% for sub in subs %}
    LEFT JOIN {{ ref(sub) }} as {{sub}}
    ON {{sub}}.id = base.id
{% endfor %}

但是,如果您的子查询实际上是在同一模型中使用 with sql 子句的子查询,则不需要使用 ref() 因为它们不是 dbt模型。您可以查看文档以更好地理解 ref()

{% set subs = ["table_1", "table_2", "table_3"] %}


SELECT
{% for sub in subs %}
    {{sub}}.* EXCEPT (id),
{% endfor %}
base.*
FROM {{ref('base')}} as base
{% for sub in subs %}
    LEFT JOIN {{ sub }}
    ON {{sub}}.id = base.id
{% endfor %}

When you use ref(), you are already inside an expression clause ({{..}}), so you do not need to add another expression clause to refer to your subs.

{% set subs = ["table_1", "table_2", "table_3"] %}


SELECT
{% for sub in subs %}
    {{sub}}.* EXCEPT (id),
{% endfor %}
base.*
FROM {{ref('base')}} as base
{% for sub in subs %}
    LEFT JOIN {{ ref(sub) }} as {{sub}}
    ON {{sub}}.id = base.id
{% endfor %}

However, if your subs are really subqueries with the use of the with sql clause in that same model, you don't need to use ref() because they are not dbt models. You can check the documentation for a better understanding of ref()

{% set subs = ["table_1", "table_2", "table_3"] %}


SELECT
{% for sub in subs %}
    {{sub}}.* EXCEPT (id),
{% endfor %}
base.*
FROM {{ref('base')}} as base
{% for sub in subs %}
    LEFT JOIN {{ sub }}
    ON {{sub}}.id = base.id
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文