dbt中宏中参数为空时的if语句

发布于 2025-01-09 13:16:12 字数 859 浏览 0 评论 0原文

我是 DBT 新手,所以也许你们可以帮我创建这个宏。我有一个宏,如下所示:

{% macro finding_number(missing_arn, acquirer_id, min_date, max_date) %}

{% set query %}

  select 
       *
  from {{ ref('hola') }}
  where event_date::date between '{{min_date}}' and '{{max_date}}'
    and acquirer_id = {{acquirer_id}} 
    and acquirer_reference_number = '{{missing_arn}}'

{% endset %}

{% set results = run_query(query) %}

{%endmacro%}

这里有什么问题?在某些情况下,我可能没有 acquirer_id 或日期来填充宏的参数。下面是一个示例:

dbt run-operation finding_number --args '{missing_arn: xyz}'

因此,如果我没有这些参数的值,并且尝试运行宏,则会出现错误,因为我没有为这些参数( acquirer_id、 min_date 和 max_date )分配任何值。 我已经看到,如果您没有为这些参数分配任何内容,可以通过使用多个 If 语句“跳转”查询中的 where 语句中的这些条件来解决此问题,但我不知道如何构建它们。对于这种情况,由于我们只有missing_arn,if语句需要“跳过”where语句中acquiser_id、min_date和max_date的条件,因为我们没有为这两个参数分配任何值,以便能够运行宏。

谢谢!

I am new using DBT so maybe you guys can help me create this macro. I have a macro as the one you can see below:

{% macro finding_number(missing_arn, acquirer_id, min_date, max_date) %}

{% set query %}

  select 
       *
  from {{ ref('hola') }}
  where event_date::date between '{{min_date}}' and '{{max_date}}'
    and acquirer_id = {{acquirer_id}} 
    and acquirer_reference_number = '{{missing_arn}}'

{% endset %}

{% set results = run_query(query) %}

{%endmacro%}

What is the problem here? That there might be some cases where I don't have an acquirer_id or a date to populate in the parameters of the macro. Find an example below:

dbt run-operation finding_number --args '{missing_arn: xyz}'

So, if I don't have a value for these parameters and I try to run the macro, it gives me an error as I haven't assigned any values to these parameters ( acquirer_id, min_date and max_date ).
I've seen this could be solve by using multiple If statements to "jump" through those conditions in the where statement inside the query if you haven't assigned anything to those parameters, but I don't know how to structure them. For this case, as we only have missing_arn, the if statement would need to "jump" over the condition of acquirer_id, min_date and max_date in the where statement, as we didn't assign any values to these two parameters in order to be able to run the macro.

Thanks!

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

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

发布评论

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

评论(1

小…楫夜泊 2025-01-16 13:16:12

我将如何构建它来快速解决这个问题......

{% macro finding_number(missing_arn, acquirer_id, min_date, max_date) %}

{% set query %}

  select 
       *
  from {{ ref('hola') }}
  where acquirer_reference_number = '{{missing_arn}}'
    {{%- if min_date is not none and max_date is not none -%}} and event_date::date between '{{min_date}}' and '{{max_date}}' {%- endif -%}
    {{%- if acquirer_id is not none -%}} and acquirer_id = {{acquirer_id}} {%- endif -%}

{% endset %}

{% set results = run_query(query) %}

{%endmacro%}

我没有包含 missing_arn 的 if 语句,因为您询问了一个场景,在该场景中您确实有这样的情况,但是,您可以应用与这样......

  where 1=1
    {{%- if missing_arn is not none -%}} and acquirer_reference_number = '{{missing_arn}}' {{%- endif -%}}

如果所有三个都丢​​失或省略了不同的参数组合,where 1=1 允许它仍然运行。
还没有测试过这个,但尝试一下。

如果您有最短日期但没有最长日期,则可选,反之亦然:

{{%- if min_date is not none -% }} and event_date::date >= '{{min_date}}' {%- endif -%}
{{%- if max_date is not none -% }} and event_date::date <= '{{max_date}}' {%- endif -%}

How I would structure it to solve this quickly…

{% macro finding_number(missing_arn, acquirer_id, min_date, max_date) %}

{% set query %}

  select 
       *
  from {{ ref('hola') }}
  where acquirer_reference_number = '{{missing_arn}}'
    {{%- if min_date is not none and max_date is not none -%}} and event_date::date between '{{min_date}}' and '{{max_date}}' {%- endif -%}
    {{%- if acquirer_id is not none -%}} and acquirer_id = {{acquirer_id}} {%- endif -%}

{% endset %}

{% set results = run_query(query) %}

{%endmacro%}

I did not include an if statement for the missing_arn because you asked about a scenario in which you did have that, HOWEVER, you could apply the same logical pattern as such…

  where 1=1
    {{%- if missing_arn is not none -%}} and acquirer_reference_number = '{{missing_arn}}' {{%- endif -%}}

The where 1=1 allows it to still run if all three are missing or different combination of parameters are left out.
Havent tested this, but give it a try.

Optional in case you have a min date but no max date or vice versa:

{{%- if min_date is not none -% }} and event_date::date >= '{{min_date}}' {%- endif -%}
{{%- if max_date is not none -% }} and event_date::date <= '{{max_date}}' {%- endif -%}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文