Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
999 views
in Technique[技术] by (71.8m points)

jinja2 - Using if block in dbt models

Apologies for asking dumb question. But I tried many different approaches but none of them seems to work.

I have a requirement to select data from 2 different tables based on the variable. I am trying to do that in dbt models with if statement but it doesn't seem to work.

Model looks something like thins:

SELECT 
*
FROM
{% if enable_whitelisting == 'true' %}
    {{ ref('accounts_whitelisted') }}    accounts
{% else %}
        {{ ref('accounts') }}   accounts
{% endif %}

Any help is appreciated.

Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I got this working eventually. Have to put the variable name within the var()

SELECT 
*
FROM
{% if var('enable_whitelisting') == 'true' %}
    {{ ref('accounts_whitelisted') }}    accounts
{% else %}
        {{ ref('accounts') }}   accounts
{% endif %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...