diff --git a/macros/sql/pivot.sql b/macros/sql/pivot.sql index 3eabc727..4cdb667c 100644 --- a/macros/sql/pivot.sql +++ b/macros/sql/pivot.sql @@ -65,22 +65,32 @@ Arguments: else_value=0, quote_identifiers=True, distinct=False) %} - {% for value in values %} - {{ agg }}( - {% if distinct %} distinct {% endif %} - case - when {{ column }} {{ cmp }} '{{ dbt.escape_single_quotes(value) }}' - then {{ then_value }} - else {{ else_value }} - end - ) - {% if alias %} - {% if quote_identifiers %} - as {{ adapter.quote(prefix ~ value ~ suffix) }} - {% else %} - as {{ dbt_utils.slugify(prefix ~ value ~ suffix) }} + + {# Check if values are empty or None #} + {% if values is none or values == [] %} + {{ log('Pivot: No values to pivot. Creating an empty column with the default then_value.', info=True) }} + {% set empty_column = prefix ~ 'empty' ~ suffix %} + {{ then_value }} as {{ adapter.quote(empty_column) }} + + {% else %} + {# Regular pivot behavior with provided values #} + {% for value in values %} + {{ agg }}( + {% if distinct %} distinct {% endif %} + case + when {{ column }} {{ cmp }} '{{ dbt.escape_single_quotes(value) }}' + then {{ then_value }} + else {{ else_value }} + end + ) + {% if alias %} + {% if quote_identifiers %} + as {{ adapter.quote(prefix ~ value ~ suffix) }} + {% else %} + as {{ dbt_utils.slugify(prefix ~ value ~ suffix) }} + {% endif %} {% endif %} - {% endif %} - {% if not loop.last %},{% endif %} - {% endfor %} + {% if not loop.last %},{% endif %} + {% endfor %} + {% endif %} {% endmacro %}