|
1 |
| -{% macro _compare_systems(system) -%} |
| 1 | +{% macro _compare_systems(coding_field, system) -%} |
2 | 2 | {#- Support both a list of systems or a single system #}
|
3 | 3 | {%- if system is string %}
|
4 |
| - c.coding.system = '{{ system }}' |
| 4 | + {{ coding_field }}.system = '{{ system }}' |
5 | 5 | {%- elif system %}
|
6 | 6 | (
|
7 | 7 | FALSE -- just here so rest of these can start with OR
|
8 | 8 | {%- for sys_info in system %}
|
9 | 9 | {#- sys_info can either by a string or a list of string then codes #}
|
10 | 10 | {%- if sys_info is string %}
|
11 |
| - OR c.coding.system = '{{ sys_info }}' |
| 11 | + OR {{ coding_field }}.system = '{{ sys_info }}' |
12 | 12 | {%- else %}
|
13 | 13 | OR (
|
14 |
| - c.coding.system = '{{ sys_info[0] }}' |
15 |
| - AND c.coding.code in ('{{ sys_info[1]|join("', '") }}') |
| 14 | + {{ coding_field }}.system = '{{ sys_info[0] }}' |
| 15 | + AND {{ coding_field }}.code in ('{{ sys_info[1]|join("', '") }}') |
16 | 16 | )
|
17 | 17 | {%- endif %}
|
18 | 18 | {%- endfor %}
|
|
48 | 48 | {%- endif %}
|
49 | 49 | WHERE
|
50 | 50 | c.coding.code IS NOT NULL
|
51 |
| - AND {{ _compare_systems(system) }} |
| 51 | + AND {{ _compare_systems("c.coding", system) }} |
52 | 52 |
|
53 | 53 | GROUP BY id
|
54 | 54 | )
|
|
72 | 72 | {%- endif %}
|
73 | 73 | WHERE
|
74 | 74 | c.coding.code IS NOT NULL
|
75 |
| - AND {{ _compare_systems(system) }} |
| 75 | + AND {{ _compare_systems("c.coding", system) }} |
76 | 76 | )
|
77 | 77 | {%- endmacro %}
|
78 | 78 |
|
|
117 | 117 |
|
118 | 118 |
|
119 | 119 | -- Extracts the category codes and returns a select with (id (string), category (string)).
|
120 |
| -{% macro extract_category_codes(src, cat_field, cat_systems) -%} |
| 120 | +{% macro extract_category_codes(src, cat_info) -%} |
121 | 121 | (
|
122 |
| -{% if cat_systems %} |
| 122 | +{%- set cat_systems = cat_info.get("cat_systems") %} |
| 123 | +{%- set cat_field = cat_info.get("cat_field") %} |
| 124 | +{%- set is_coding = cat_info.get("is_coding", False) %} |
| 125 | +{% if is_coding %} |
| 126 | + {# TODO: handle array & no-systems cases. But this works for now. #} |
| 127 | + SELECT id, {{ cat_field }}.code AS category |
| 128 | + FROM {{ src }} |
| 129 | + WHERE {{ _compare_systems(cat_field, cat_systems) }} |
| 130 | +{% elif cat_systems %} |
123 | 131 | WITH
|
124 | 132 | array_codes AS {{ extract_codes(src, cat_field, system=cat_systems, is_array=true) }}
|
125 | 133 | SELECT id, {{ array_to_string('codes') }} AS category
|
|
0 commit comments