Skip to content

Commit

Permalink
Loop twice
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-finley committed Sep 27, 2023
1 parent a03b219 commit 1677fd5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion macros/remove_duplicate_and_prefix_from_columns.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{% 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 -%}
{%- do duplicate_exclude.append(col.name) -%}
{%- 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:] }}
Expand All @@ -16,4 +21,4 @@
{%- endif -%}
{% endfor %}

{% endmacro %}
{% endmacro %}

0 comments on commit 1677fd5

Please sign in to comment.