dbt中宏中参数为空时的if语句
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将如何构建它来快速解决这个问题......
我没有包含
missing_arn
的 if 语句,因为您询问了一个场景,在该场景中您确实有这样的情况,但是,您可以应用与这样......如果所有三个都丢失或省略了不同的参数组合,
where 1=1
允许它仍然运行。还没有测试过这个,但尝试一下。
如果您有最短日期但没有最长日期,则可选,反之亦然:
How I would structure it to solve this quickly…
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…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: