-- noqa: disable=all
/*
This is a reference output of the SQL generated by builder_code_detection.py 
<<<<<<< HEAD
that is used by the core__encounter_type table, against the synthea dataset.
It is provided as a form of documentation only and will not be invoked directly.
*/

CREATE TABLE discovery__code_sources AS
SELECT DISTINCT
    'condition' AS table_name,
    'category' AS column_name,
    t2.row2.code,
    t2.row2.display,
    t2.row2.system
FROM condition,
UNNEST(category) AS t1 (row1),
UNNEST(t1.row1.coding) AS t2 (row2)
UNION
SELECT DISTINCT
    'condition' AS table_name,
    'code' AS column_name,
    t.row.code,
    t.row.display,
    t.row.system
FROM condition,
UNNEST(code.coding) AS t (row)
UNION
SELECT DISTINCT
    'documentreference' AS table_name,
    'type' AS column_name,
    t.row.code,
    t.row.display,
    t.row.system
FROM documentreference,
UNNEST(type.coding) AS t (row)
UNION
SELECT DISTINCT
    'documentreference' AS table_name,
    'category' AS column_name,
    t2.row2.code,
    t2.row2.display,
    t2.row2.system
FROM documentreference,
UNNEST(category) AS t1 (row1),
UNNEST(t1.row1.coding) AS t2 (row2)
UNION
SELECT * 
    FROM ( 
        VALUES (
            ('encounter','class', '', '', '')
        )
    )
AS t ( table_name, column_name, code, display, system ) -- noqa: L025
UNION
SELECT DISTINCT
    'encounter' AS table_name,
    'type' AS column_name,
    t2.row2.code,
    t2.row2.display,
    t2.row2.system
FROM encounter,
UNNEST(type) AS t1 (row1),
UNNEST(t1.row1.coding) AS t2 (row2)
UNION
SELECT * 
    FROM ( 
        VALUES (
            ('encounter','servicetype', '', '', '')
        )
    )
AS t ( table_name, column_name, code, display, system ) -- noqa: L025
UNION
SELECT * 
    FROM ( 
        VALUES (
            ('encounter','priority', '', '', '')
        )
    )
AS t ( table_name, column_name, code, display, system ) -- noqa: L025
UNION
SELECT DISTINCT
    'encounter' AS table_name,
    'reasoncode' AS column_name,
    t2.row2.code,
    t2.row2.display,
    t2.row2.system
FROM encounter,
UNNEST(reasoncode) AS t1 (row1),
UNNEST(t1.row1.coding) AS t2 (row2)
UNION
SELECT DISTINCT
    'medication' AS table_name,
    'code' AS column_name,
    t.row.code,
    t.row.display,
    t.row.system
FROM medication,
UNNEST(code.coding) AS t (row)
UNION
SELECT DISTINCT
    'observation' AS table_name,
    'category' AS column_name,
    t2.row2.code,
    t2.row2.display,
    t2.row2.system
FROM observation,
UNNEST(category) AS t1 (row1),
UNNEST(t1.row1.coding) AS t2 (row2)
UNION
SELECT DISTINCT
    'observation' AS table_name,
    'code' AS column_name,
    t.row.code,
    t.row.display,
    t.row.system
FROM observation,
UNNEST(code.coding) AS t (row)
UNION
SELECT DISTINCT
    'patient' AS table_name,
    'maritalstatus' AS column_name,
    t.row.code,
    t.row.display,
    t.row.system
FROM patient,
UNNEST(maritalstatus.coding) AS t (row)
