run_query returns a table, you have to specify the column and row you want.
To do that you can return the column as a list, have a look at this example:
{% set payment_methods_query %}
select distinct payment_method from app_data.payments
order by 1
{% endset %}
{% set results = run_query(payment_methods_query) %}
{% if execute %}
{# Return the first column #}
{% set results_list = results.columns[0].values() %}
{% else %}
{% set results_list = [] %}
{% endif %}
select
order_id,
{% for payment_method in results_list %}
sum(case when payment_method = '{{ payment_method }}' then amount end) as {{ payment_method }}_amount,
{% endfor %}
sum(amount) as total_amount
from {{ ref('raw_payments') }}
group by 1
Thank you @brunoszdl My query returns a scalar value. like max of date. Do i still need to follow these steps. Is there is a way to get the resultset stored directly into a variable as it a scalar resultset?
@rjames , even if your select returns a scalar, the run_queryfunction returns a table, with one column and one row.
So, yes, the only way I know is by doing all of these steps, don’t know a workaround.
If you want to keep it all in one line you try something like
{% set your_var = run_query("select nvl(max(etl_batch_id) + 1, 1000) as etl_batch_id from {{ this }}").columns[0].values()[0] %}
Maybe you have to put it inside a {% if execute() %} condition like
{% if execute %}
{% set your_var = run_query("select nvl(max(etl_batch_id) + 1, 1000) as etl_batch_id from {{ this }}").columns[0].values()[0] %}
{% endif %}
But try without it first.
I did not test it, so maybe you need to adjust something.
I would recommend separating it into multiple lines, so it is easier to debug.
{% set your_query %}
select nvl(max(etl_batch_id) + 1, 1000) as etl_batch_id from {{ this }}
{% endset %}
{% set results = run_query(your_query) %}
{% if execute %}
{% set results_list = results.columns[0].values() %}
{% endif %}
{% set result = results_list[0] %}