CREATE TABLE {{ target_table }} AS (
    WITH
    {%- for system in ext_systems %}

    system_{{ system }} AS (
        SELECT DISTINCT
            s.{{ source_id }} AS id,
            '{{ loop.index0 }}' AS priority,
            '{{ system }}' AS system, -- noqa: RF04
            ext_child.ext.valuecoding.code AS {{ target_col_prefix }}_code,
            ext_child.ext.valuecoding.display AS {{ target_col_prefix }}_display
        FROM
            {{ source_table }} AS s,
            UNNEST(extension) AS ext_parent (ext),
            UNNEST(ext_parent.ext.extension) AS ext_child (ext)
        WHERE
            ext_parent.ext.url = '{{ fhir_extension }}'
            AND ext_child.ext.url = '{{ system }}'
            AND ext_child.ext.valuecoding.display != ''
    ), --noqa: LT07
    {%- endfor %}

    union_table AS (
        {%- for system in ext_systems %}
        SELECT
            id,
            priority,
            system,
            {{ target_col_prefix }}_code,
            {{ target_col_prefix }}_display
        FROM system_{{ system }}
        {%- if not loop.last %}
        UNION 
        {%- endif -%}
        {%- endfor %}
        ORDER BY id, priority
    )

    SELECT
        id,
        system,
        {{ target_col_prefix }}_code,
        {{ target_col_prefix }}_display
    FROM (
        SELECT
            id,
            system,
            ARRAY_AGG({{ target_col_prefix }}_code) AS {{ target_col_prefix }}_code,
            ARRAY_AGG(
                {{ target_col_prefix }}_display
            ) AS {{ target_col_prefix }}_display,
            ROW_NUMBER()
            OVER (
                PARTITION BY id, system
            ) AS available_priority
        FROM union_table
        GROUP BY id, system
    )
    WHERE available_priority = 1
);
