for 循环中的 DBT 引用
我的 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}}”的节点,但未找到
以下是我读过的一些页面,但找不到解决方案
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
ref()
时,您已经位于表达式子句 ({{..}}
) 内,因此您不需要添加另一个表达式子句来引用你的潜艇。但是,如果您的子查询实际上是在同一模型中使用
with
sql 子句的子查询,则不需要使用ref()
因为它们不是 dbt模型。您可以查看文档以更好地理解ref()
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.However, if your subs are really subqueries with the use of the
with
sql clause in that same model, you don't need to useref()
because they are not dbt models. You can check the documentation for a better understanding ofref()