From 1677fd53952910dfba9e545f0b08468d144f5f39 Mon Sep 17 00:00:00 2001 From: Greg Finley Date: Wed, 27 Sep 2023 12:06:28 -0700 Subject: [PATCH] Loop twice --- macros/remove_duplicate_and_prefix_from_columns.sql | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/macros/remove_duplicate_and_prefix_from_columns.sql b/macros/remove_duplicate_and_prefix_from_columns.sql index ed6630e..0f8f53e 100644 --- a/macros/remove_duplicate_and_prefix_from_columns.sql +++ b/macros/remove_duplicate_and_prefix_from_columns.sql @@ -1,5 +1,6 @@ {% macro remove_duplicate_and_prefix_from_columns(columns, prefix='', exclude=[]) %} + {# Loop once to find duplicates #} {%- set duplicate_exclude = [] -%} {% for col in columns if col.name not in exclude %} {%- for dupe in columns if col.name[prefix|length:]|lower == dupe.name|lower -%} @@ -7,6 +8,10 @@ {%- do duplicate_exclude.append(dupe.name) -%} , coalesce({{ col.name }}, {{dupe.name}}) as {{ col.name[prefix|length:] }} {%- endfor %} + {% endfor %} + + {# Loop again to find non-duplicates #} + {% for col in columns if col.name not in exclude %} {%- if col.name|lower not in duplicate_exclude|lower -%} {% if col.name[:prefix|length]|lower == prefix %} , {{ col.name }} as {{ col.name[prefix|length:] }} @@ -16,4 +21,4 @@ {%- endif -%} {% endfor %} -{% endmacro %} \ No newline at end of file +{% endmacro %}