The problem I’m having
Hi, I am trying to set query and call run_query()
in a dbt macro called get_records
but unable to set the query correctly using the argument in the where
clause.
The context of why I’m trying to do this
I have a struct field in a table and for every row (ID - a string field) , I want to extract and loop over the struct field and return a conditional outcome.
What I’ve already tried
I am calling a macro called get_records
in which I am calling the run_query
so I can pass the ID
and run Query against it to get the result of result field and iterate over. Macro is as follow:
{% macro get_records(lead_id) %}
{% set sql_statement %}
select lead_record from {{ ref('source_table') }} where lead_id = {{ lead_id }} limit 1
{% endset %}
{{ print(query) }}
{% if execute %}
{% set results = run_query(sql_statement) %}
{% set records = results.columns[0].values() %}
{% else %}
{% set records = [] %}
{% endif %}
-- do something here
{% endmacro %}
and the call to macro is as:
select
lead_id,
{{ get_reasons(lead_id='lead_id') }} as filered_record
from {{ ref('source_table') }}
But the sql_statement
getting compiled as select lead_record from {{ ref('source_table') }} where lead_id = lead_id
which is wrong. Is there a way to set a query using the argument value in where clause or attempt this in a another way ? thanks!