Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for excluding last column in unpivot macro #83

Merged
merged 2 commits into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions macros/sql/unpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Arguments:

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

{%- set include_cols = [] %}

{%- set table_columns = {} %}

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

{%- for col in cols -%}

{%- if col.column not in exclude -%}
select
{%- for exclude_col in exclude %}
{{ exclude_col }},
{%- endfor %}
cast('{{ col.column }}' as varchar) as field_name,
{{ dbt_utils.safe_cast(field=col.column, type=cast_to) }} as value
from {{ table }}
{% if not loop.last -%}
union all
{% endif -%}
{%- endif -%}
{%- if col.column not in exclude -%}
{% set _ = include_cols.append(col) %}
{%- endif %}
{%- endfor %}

{%- for col in include_cols -%}

select
{%- for exclude_col in exclude %}
{{ exclude_col }},
{%- endfor %}
cast('{{ col.column }}' as varchar) as field_name,
{{ dbt_utils.safe_cast(field=col.column, type=cast_to) }} as value
from {{ table }}
{% if not loop.last -%}
union all
{% endif -%}
{%- endfor -%}
{%- endmacro %}