Skip to content

Commit 820aadb

Browse files
authored
Merge pull request #83 from alexcampana/unpivot-fix
fix for excluding last column in unpivot macro
2 parents d0a90fe + 0083a57 commit 820aadb

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

macros/sql/unpivot.sql

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Arguments:
1313

1414
{%- set exclude = exclude if exclude is not none else [] %}
1515

16+
{%- set include_cols = [] %}
17+
1618
{%- set table_columns = {} %}
1719

1820
{%- set _ = table_columns.update({table: []}) %}
@@ -26,18 +28,22 @@ Arguments:
2628
{%- set cols = adapter.get_columns_in_table(schema, table_name) %}
2729

2830
{%- for col in cols -%}
29-
30-
{%- if col.column not in exclude -%}
31-
select
32-
{%- for exclude_col in exclude %}
33-
{{ exclude_col }},
34-
{%- endfor %}
35-
cast('{{ col.column }}' as varchar) as field_name,
36-
{{ dbt_utils.safe_cast(field=col.column, type=cast_to) }} as value
37-
from {{ table }}
38-
{% if not loop.last -%}
39-
union all
40-
{% endif -%}
41-
{%- endif -%}
31+
{%- if col.column not in exclude -%}
32+
{% set _ = include_cols.append(col) %}
33+
{%- endif %}
34+
{%- endfor %}
35+
36+
{%- for col in include_cols -%}
37+
38+
select
39+
{%- for exclude_col in exclude %}
40+
{{ exclude_col }},
41+
{%- endfor %}
42+
cast('{{ col.column }}' as varchar) as field_name,
43+
{{ dbt_utils.safe_cast(field=col.column, type=cast_to) }} as value
44+
from {{ table }}
45+
{% if not loop.last -%}
46+
union all
47+
{% endif -%}
4248
{%- endfor -%}
4349
{%- endmacro %}

0 commit comments

Comments
 (0)