CREATE TABLE {{ target_table }} AS (
    WITH
    {%- for system in cc_column.code_systems %}
    system_{{ cc_column.name }}_{{ loop.index0 }} AS (
        SELECT DISTINCT
            s.{{ source_id }} AS id,
            '{{ loop.index0 }}' AS priority,
            u.codeable_concept.code AS code,
            u.codeable_concept.display AS display,
            u.codeable_concept.system AS code_system
        FROM
            {{ source_table }} AS s,
        {% if cc_column.is_array %}
            UNNEST(s.{{ cc_column.name }}) AS cc (cc_row),
            UNNEST(cc.cc_row.coding) AS u (codeable_concept)
        {% else %}
            UNNEST(s.{{ cc_column.name }}.coding) AS u (codeable_concept)
        {%- endif %}
        WHERE
            u.codeable_concept.system = '{{ system }}'
    ), --noqa: LT07
    {%- endfor %}

    union_table AS (
        {%- for system in cc_column.code_systems %}
        SELECT
            id,
            priority,
            code_system,
            code,
            display
        FROM system_{{ cc_column.name }}_{{ loop.index0 }}
        {%- if not loop.last %}
        UNION 
        {%- endif -%}
        {%- endfor %}
    ),

    partitioned_table AS (
        SELECT
            id,
            code,
            code_system,
            display,
            priority,
            ROW_NUMBER()
            OVER (
                PARTITION BY id
            ) AS available_priority
        FROM union_table
        GROUP BY id, priority, code_system, code, display
        ORDER BY priority ASC
    )

    SELECT
        id,
        code,
        code_system,
        display
    FROM partitioned_table
    WHERE available_priority = 1
);
