{#- 
    Macros
-#}
{%- macro comma_delineate(loop) -%}
{%- if not loop.last -%}
, 
{%- endif -%}
{%- endmacro -%}
{%- macro select_column_or_alias(join_cols_by_table) -%}
{%- for key in join_cols_by_table %}
{%- for column in join_cols_by_table[key]["included_cols"] %}
{%- if column|length == 1 %}
        "{{ key }}"."{{ column[0] }}",
        {%- else %}
        "{{ key }}"."{{ column[0] }}" AS "{{ column[1] }}",
{%- endif %}
{%- endfor %}
{%- endfor %}    
{%- endmacro -%}
{#- 
    Query
-#}
CREATE TABLE {{ target_table }} AS (
    SELECT DISTINCT
        sample_cohort."{{ primary_ref }}",
        sample_cohort."{{ dependent_variable }}",
        {%- if count_table %}
        (
            SELECT COUNT(DISTINCT {{ primary_ref }})
            FROM "{{ count_table }}"
            WHERE sample_cohort."{{ count_ref }}" = "{{ count_table }}"."{{ count_ref }}"
        ) AS instance_count,
        {%- endif -%}
        {{ select_column_or_alias(join_cols_by_table) }}
        {{ neg_source_table }}.code
    FROM "{{ pos_source_table }}_sampled_ids_{{ table_suffix }}" AS sample_cohort,
        "{{ neg_source_table }}", 
    {%- for key in join_cols_by_table %}
        "{{ key }}"
    {{- comma_delineate(loop) -}}
    {% endfor %}
    WHERE
        sample_cohort."{{ primary_ref }}" = "{{ neg_source_table }}"."{{ primary_ref }}"
    {%- for key in join_cols_by_table %}
    AND sample_cohort."{{ join_cols_by_table[key]["join_id"] }}"
    = "{{ key }}"."{{ join_cols_by_table[key]["join_id"] }}"
{{- comma_delineate(loop) -}}
    {% endfor %}
    ORDER BY sample_cohort."{{ primary_ref }}"
)
