-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathunion.sql
More file actions
33 lines (31 loc) · 1.32 KB
/
union.sql
File metadata and controls
33 lines (31 loc) · 1.32 KB
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
{%- macro union(relations) -%}
{#
relations: a list of relations (model/ source tables) that exist in the database.
This macro unions any number of tables, aligning columns by name and filling missing columns with NULLs.
#}
{%- set joint_columns = [] -%}
{%- for relation in relations -%}
{%- set cols = adapter.get_columns_in_relation(relation) -%}
{%- for col in cols -%}
{%- if col.name not in joint_columns -%}
{%- do joint_columns.append(col.name) -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- set selects = [] -%}
{%- for relation in relations -%}
{%- set cols = adapter.get_columns_in_relation(relation) -%}
{%- set col_names = cols | map(attribute='name') | list -%}
{%- set select_parts = [] -%}
{%- for col in joint_columns -%}
{%- if col in col_names -%}
{%- do select_parts.append('"' ~ col ~ '"') -%}
{%- else -%}
{%- do select_parts.append('NULL as "' ~ col ~ '"') -%}
{%- endif -%}
{%- endfor -%}
{%- set select_sql = 'select ' ~ select_parts | join(', ') ~ ' from ' ~ relation -%}
{%- do selects.append(select_sql) -%}
{%- endfor -%}
{{ selects | join('\nunion all\n') }}
{%- endmacro -%}