-- noqa: disable=all
-- This sql was autogenerated as a reference example using the library
-- CLI. Its format is tied to the specific database it was run against,
-- and it may not be correct for all databases. Use the CLI's build 
-- option to derive the best SQL for your dataset.

-- ###########################################################

CREATE TABLE discovery__code_sources AS
SELECT DISTINCT
    'allergyintolerance' AS table_name,
    'clinicalstatus' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM allergyintolerance,
UNNEST(clinicalstatus.coding) AS table_1 (col_1)

UNION ALL

SELECT DISTINCT
    'allergyintolerance' AS table_name,
    'verificationstatus' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM allergyintolerance,
UNNEST(verificationstatus.coding) AS table_1 (col_1)

UNION ALL

SELECT DISTINCT
    'allergyintolerance' AS table_name,
    'code' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM allergyintolerance,
UNNEST(code.coding) AS table_1 (col_1)

UNION ALL

SELECT *
FROM (
    VALUES (
        'allergyintolerance',
        'reaction.substance',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'allergyintolerance',
        'reaction.manifestation',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'allergyintolerance',
        'reaction.exposureroute',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT DISTINCT
    'condition' AS table_name,
    'category' AS column_name,
    table_2.col_2.code,
    table_2.col_2.display,
    table_2.col_2.system
FROM condition,
UNNEST(category) AS table_1 (col_1),
UNNEST(col_1.coding) as table_2 (col_2)

UNION ALL

SELECT DISTINCT
    'condition' AS table_name,
    'code' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM condition,
UNNEST(code.coding) AS table_1 (col_1)

UNION ALL

SELECT DISTINCT
    'diagnosticreport' AS table_name,
    'category' AS column_name,
    table_2.col_2.code,
    table_2.col_2.display,
    table_2.col_2.system
FROM diagnosticreport,
UNNEST(category) AS table_1 (col_1),
UNNEST(col_1.coding) as table_2 (col_2)

UNION ALL

SELECT DISTINCT
    'diagnosticreport' AS table_name,
    'code' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM diagnosticreport,
UNNEST(code.coding) AS table_1 (col_1)

UNION ALL

SELECT *
FROM (
    VALUES (
        'diagnosticreport',
        'conclusioncode',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT DISTINCT
    'documentreference' AS table_name,
    'type' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM documentreference,
UNNEST(type.coding) AS table_1 (col_1)

UNION ALL

SELECT DISTINCT
    'documentreference' AS table_name,
    'category' AS column_name,
    table_2.col_2.code,
    table_2.col_2.display,
    table_2.col_2.system
FROM documentreference,
UNNEST(category) AS table_1 (col_1),
UNNEST(col_1.coding) as table_2 (col_2)

UNION ALL

SELECT DISTINCT
    'encounter' AS table_name,
    'class' AS column_name,
    class.code,
    class.display,
    class.system
FROM encounter

UNION ALL

SELECT DISTINCT
    'encounter' AS table_name,
    'type' AS column_name,
    table_2.col_2.code,
    table_2.col_2.display,
    table_2.col_2.system
FROM encounter,
UNNEST(type) AS table_1 (col_1),
UNNEST(col_1.coding) as table_2 (col_2)

UNION ALL

SELECT *
FROM (
    VALUES (
        'encounter',
        'servicetype',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'encounter',
        'priority',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT DISTINCT
    'encounter' AS table_name,
    'reasoncode' AS column_name,
    table_2.col_2.code,
    table_2.col_2.display,
    table_2.col_2.system
FROM encounter,
UNNEST(reasoncode) AS table_1 (col_1),
UNNEST(col_1.coding) as table_2 (col_2)

UNION ALL

SELECT DISTINCT
    'encounter' AS table_name,
    'hospitalization.dischargedisposition' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM encounter,
UNNEST(hospitalization.dischargedisposition.coding) AS table_1 (col_1)

UNION ALL

SELECT *
FROM (
    VALUES (
        'medication',
        'codecodeableconcept',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'medication',
        'medicationcode',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT DISTINCT
    'medicationrequest' AS table_name,
    'medicationcodeableconcept' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM medicationrequest,
UNNEST(medicationcodeableconcept.coding) AS table_1 (col_1)

UNION ALL

SELECT DISTINCT
    'observation' AS table_name,
    'category' AS column_name,
    table_2.col_2.code,
    table_2.col_2.display,
    table_2.col_2.system
FROM observation,
UNNEST(category) AS table_1 (col_1),
UNNEST(col_1.coding) as table_2 (col_2)

UNION ALL

SELECT DISTINCT
    'observation' AS table_name,
    'code' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM observation,
UNNEST(code.coding) AS table_1 (col_1)

UNION ALL

SELECT *
FROM (
    VALUES (
        'observation',
        'interpretation',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT DISTINCT
    'observation' AS table_name,
    'valuecodeableconcept' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM observation,
UNNEST(valuecodeableconcept.coding) AS table_1 (col_1)

UNION ALL

SELECT *
FROM (
    VALUES (
        'observation',
        'dataabsentreason',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT DISTINCT
    'patient' AS table_name,
    'maritalstatus' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM patient,
UNNEST(maritalstatus.coding) AS table_1 (col_1)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'statusreason',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'category',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT DISTINCT
    'procedure' AS table_name,
    'code' AS column_name,
    table_1.col_1.code,
    table_1.col_1.display,
    table_1.col_1.system
FROM procedure,
UNNEST(code.coding) AS table_1 (col_1)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'performer.function',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'reasoncode',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'bodysite',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'outcome',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'complication',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'followup',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'focalDevice.action',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)

UNION ALL

SELECT *
FROM (
    VALUES (
        'procedure',
        'usedcode',
        '',
        '',
        ''
    )
)
    AS t (table_name, column_name, code, display, system)



