【问题标题】:Is there a way to write a Jinja set nested within a Jinja for loop有没有办法编写嵌套在 Jinja for 循环中的 Jinja 集
【发布时间】:2022-11-18 03:18:08
【问题描述】:

我目前在 DBT 中使用 Jinja 来创建这 3 组,它们都可以正常工作:

{% set value1 %}
    select
            category1,
            category2,
            category3,
            category4
        
        from {{ ref('table') }}
        where category1 = 'value1'
        order by category2
    
{% endset %}

{% set value2 %}
    select
            category1,
            category2,
            category3,
            category4
        
        from {{ ref('table') }}
        where category1 = value2
        order by category2
    

{% endset %}



{% set value3%}
    select
            category1,
            category2,
            category3,
            category4
        
        from {{ ref('table') }}
        where category1 = value3
        order by category2
    

{% endset %}

我想知道是否有一种方法可以通过使用 for 循环来压缩这 3 个集合的创建。

这是我这样做的尝试:

{% set attributes = ('value1', 'value2', 'value3') %}


{% for i in attributes%}
    
    {% set {{i}} %}
    
        select
            category1,
            category2,
            category3,
            category4
        
        from {{ ref('table') }}
        where category1 = {{i}}
        order by category2
    
    {% endset %}
    
{% endfor %}

我一直在 for 循环中遇到关于集合第一行的错误。我尝试用 for 循环中的语法更改以多种方式编写它,但没有任何效果。我想知道我是否可以重新创建 3 组的输出,这会比写出所有 3 组更屈尊?

【问题讨论】:

    标签: jinja2 dbt


    【解决方案1】:

    Don't Nest Your Curlies

    (应该是set i而不是set {{i}})。

    {% set attributes = ('value1', 'value2', 'value3') %}
    
    
    {% for i in attributes %}
        
        {% set i %}
        
            select
                category1,
                category2,
                category3,
                category4
            
            from {{ ref('table') }}
            where category1 = {{i}}
            order by category2
        
        {% endset %}
        
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      相关资源
      最近更新 更多