简体   繁体   中英

Concatenate columns using a macro in DBT for Redshift

I want to concatenate a few columns using column1 ^^ column2 ^^ ... syntax in DBT for Redshift. If there are NULL values in the columns @@ should be used, resulting in fe @@ ^^ @@ . I have found the following macro for concatenation:

{% macro safe_concat(field_list) %}
  {# Takes an input list and generates a concat() statement with each argument in the list safe_casted to a string and wrapped in an ifnull() #}
  concat({% for f in field_list %}
    ifnull(safe_cast({{ f }} as string), '@@')
    {% if not loop.last %}, {% endif %}
  {% endfor %})
{% endmacro %}

When I use it in my select statement:

select
  {{ safe_concat([street, city]) }} as address_key
from source

I get the following error. Is this related to the code I am using?

Database Error in model address (models/address.sql)
  syntax error at or near "as"
  LINE 32:     ifnull(safe_cast( as string), '@@')

当您在宏中调用列名时,尝试将它们用引号括起来 - 我认为它试图传入变量streetcity (因为您已经在花括号内),它们不存在,因此评估为None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM