使用SQL和Jinja的DBT宏

发布于 2025-01-22 03:34:26 字数 672 浏览 0 评论 0原文

我已经写了DBT宏,并在尝试部署时会出现错误。我认为它不会转换为适当的SQL。

momcalc.sql

{% macro MoMCalc(val,Effectivedate,Frequency) %}
case
when "Frequency" LIKE '%Weekly%' then {{val}} - LAG({{val}}, 4) OVER (Order by '{{Effectivedate}}')
when "Frequency" LIKE '%Monthly%' then {{val}} - LAG({{val}}, 1) OVER (Order by '{{Effectivedate}}')
end
{% endmacro %}

错误 -

syntax error line 32 at position 0 unexpected 'case'.
20:33:23    syntax error line 33 at position 53 unexpected 'Value'.
20:33:23    syntax error line 33 at position 63 unexpected 'OVER'.
20:33:23    syntax error line 33 at position 88 unexpected 'Date'

I have written the DBT macro and getting error when trying to deploy. I think it's not getting converted to proper sql.

MoMCalc.sql

{% macro MoMCalc(val,Effectivedate,Frequency) %}
case
when "Frequency" LIKE '%Weekly%' then {{val}} - LAG({{val}}, 4) OVER (Order by '{{Effectivedate}}')
when "Frequency" LIKE '%Monthly%' then {{val}} - LAG({{val}}, 1) OVER (Order by '{{Effectivedate}}')
end
{% endmacro %}

Error -

syntax error line 32 at position 0 unexpected 'case'.
20:33:23    syntax error line 33 at position 53 unexpected 'Value'.
20:33:23    syntax error line 33 at position 63 unexpected 'OVER'.
20:33:23    syntax error line 33 at position 88 unexpected 'Date'

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

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

发布评论

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

评论(1

一百个冬季 2025-01-29 03:34:26

您可以在target/Commied/目录中查看编译代码。 (您需要查找调用此宏的模型文件的编译版本)。这会让您了解出了什么问题。

我实际上认为,在您在模型文件中调用宏之前,您可能只是缺少逗号?

我的另一个猜测(有限的上下文)是,您没有在宏中使用curlies围绕频率的参数使用。假设您需要在SQL 中围绕传递到频率的值中的双引号

{% macro MoMCalc(val, Effectivedate, Frequency) %}
case
    when "{{ Frequency }}" like '%Weekly%'
    then {{ val }} - lag({{ val }}, 4) over (order by '{{Effectivedate}}')
    when "{{ Frequency }}" like '%Monthly%'
    then {{ val }} - lag({{ val }}, 1) over (order by '{{Effectivedate}}')
end
{% endmacro %}

然后调用要使用的宏:

select
    another_field,
    MoMCalc('value_field', 'effective_date_field', 'frequency_field') as mom_calc

You can view the compiled code in the target/compiled/ directory. (You'll want to look for the compiled version of the model file where you call this macro). That'll give you a sense for what went wrong.

I actually think you might just be missing a comma before you call the macro, in your model file?

My other guess (with limited context) is that you're not using curlies around the argument named Frequency in your macro. This should work, assuming you want double quotes in the SQL around the value passed into Frequency:

{% macro MoMCalc(val, Effectivedate, Frequency) %}
case
    when "{{ Frequency }}" like '%Weekly%'
    then {{ val }} - lag({{ val }}, 4) over (order by '{{Effectivedate}}')
    when "{{ Frequency }}" like '%Monthly%'
    then {{ val }} - lag({{ val }}, 1) over (order by '{{Effectivedate}}')
end
{% endmacro %}

Then to call that macro you want to use:

select
    another_field,
    MoMCalc('value_field', 'effective_date_field', 'frequency_field') as mom_calc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文