forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivot.sql
100 lines (87 loc) · 3.08 KB
/
pivot.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{#
Pivot values from rows to columns.
Example:
Input: 'public.test'
| size | color |
|------+-------|
| S | red |
| S | blue |
| S | red |
| M | red |
select
size,
{{ dbt_utils.pivot('color', dbt_utils.get_column_values('public.test',
'color')) }}
from public.test
group by size
Output:
| size | red | blue |
|------+-----+------|
| S | 2 | 1 |
| M | 1 | 0 |
Arguments:
column: Column name, required
values: List of row values to turn into columns, required
excluded_values: list of values you do not want pivoted. This should be
passed in parentheses ie excluded_values=('foo','bar').
default is ['']
alias: Whether to create column aliases, default is True
coalesce: Whether the whole statement should coalesce to 0
ie coalesce(agg(case),0) default is False
agg: SQL aggregation function, default is sum
total: Whether all values (except excluded) should be summed for
a total column. default is False
distinct: Whether vals in the agg should be distinct ie count(distinct).
default is False
cmp: SQL value comparison, default is =
prefix: Column alias prefix, default is blank
suffix: Column alias postfix, default is blank
then_value: Value to use if comparison succeeds, default is 1
else_value: Value to use if comparison fails, default is 0
#}
{% macro pivot(column,
values,
excluded_values=[''],
alias=True,
distinct=False,
coalesce=False,
total=False,
agg='sum',
cmp='=',
prefix='',
suffix='',
then_value=1,
else_value='null') %}
{%- set filtered_list=[] -%}
{%- for v in values -%}
{%- if v|string not in excluded_values -%}
{%- set a= filtered_list.append(v) -%}
{%- endif -%}
{%- endfor -%}
{% for v in filtered_list %}
{%- if not loop.first -%},{%- endif -%}
{%- if coalesce -%} coalesce( {%- endif -%}
{{ agg }}({% if distinct %} distinct {% endif %}
case
when {{ column }} {{ cmp }} '{{ v }}'
then {{ then_value }}
else {{ else_value }}
end
) {% if coalesce %} ,0) {% endif %}
{% if alias %} as {{ adapter.quote(prefix ~ v ~ suffix)|replace('-','_') }} {% endif %}
{% endfor %}
{%- if total -%}
,{% if coalesce %} coalesce( {% endif %}{{ agg }}({% if distinct %} distinct {% endif %}
case
when {{ column }} not in (
{%- for ev in excluded_values -%}
'{{ ev }}'
{%- if not loop.last -%},{%- endif -%}
{% endfor %})
then {{ then_value }}
else {{ else_value }}
end
) {% if coalesce %} ,0) {% endif %}
{% if alias %} as {{ adapter.quote(prefix ~ 'total' ~ suffix)|replace('-','_') }}{% endif %}
{%- endif -%}
{% endmacro %}