-- 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 core__observation_dn_category AS (
    WITH

    system_category_0 AS (
        SELECT DISTINCT
            s.id AS id,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            observation AS s,
            UNNEST(s.category) AS cc (cc_row),
            UNNEST(cc.cc_row.coding) AS u (codeable_concept)
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            code_system,
            code,
            display
        FROM system_category_0
        
    )
    SELECT
        id,
        code,
        code_system,
        display
    FROM union_table
);


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

CREATE TABLE core__observation_dn_code AS (
    WITH

    system_code_0 AS (
        SELECT DISTINCT
            s.id AS id,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            observation AS s,
            UNNEST(s.code.coding) AS u (codeable_concept)
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            code_system,
            code,
            display
        FROM system_code_0
        
    )
    SELECT
        id,
        code,
        code_system,
        display
    FROM union_table
);


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

CREATE TABLE IF NOT EXISTS "main"."core__observation_dn_interpretation"
AS (
    SELECT * FROM (
        VALUES
        (cast(NULL AS varchar),cast(NULL AS varchar),cast(NULL AS varchar),cast(NULL AS varchar))
    )
        AS t ("id","code","code_system","display")
);

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

CREATE TABLE core__observation_dn_valuecodeableconcept AS (
    WITH

    system_valuecodeableconcept_0 AS (
        SELECT DISTINCT
            s.id AS id,
            u.codeable_concept.code,
            u.codeable_concept.display,
            u.codeable_concept.system AS code_system
        FROM
            observation AS s,
            UNNEST(s.valuecodeableconcept.coding) AS u (codeable_concept)
    ), --noqa: LT07

    union_table AS (
        SELECT
            id,
            code_system,
            code,
            display
        FROM system_valuecodeableconcept_0
        
    )
    SELECT
        id,
        code,
        code_system,
        display
    FROM union_table
);


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

CREATE TABLE IF NOT EXISTS "main"."core__observation_dn_dataabsentreason"
AS (
    SELECT * FROM (
        VALUES
        (cast(NULL AS varchar),cast(NULL AS varchar),cast(NULL AS varchar),cast(NULL AS varchar))
    )
        AS t ("id","code","code_system","display")
);

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



CREATE TABLE core__observation AS
WITH temp_observation AS (
    SELECT
        o.id,
        o.status,
        o.valueString,
        o.valueQuantity.value AS valuequantity_value,
        o.valueQuantity.comparator AS valuequantity_comparator,
        o.valueQuantity.unit AS valuequantity_unit,
        o.valueQuantity.system AS valuequantity_system,
        o.valueQuantity.code AS valuequantity_code,
        date_trunc('day', date(from_iso8601_timestamp(o."effectiveDateTime")))
            AS effectiveDateTime_day,
        date_trunc('week', date(from_iso8601_timestamp(o."effectiveDateTime")))
            AS effectiveDateTime_week,
        date_trunc('month', date(from_iso8601_timestamp(o."effectiveDateTime")))
            AS effectiveDateTime_month,
        date_trunc('year', date(from_iso8601_timestamp(o."effectiveDateTime")))
            AS effectiveDateTime_year,
        odc.code AS observation_code,
        odc.code_system AS observation_code_system,
        odcat.code AS category_code,
        odcat.code_system AS category_code_system,
        odi.code AS interpretation_code,
        odi.code_system AS interpretation_code_system,
        odi.display AS interpretation_display,
        odvcc.code AS valueCodeableConcept_code,
        odvcc.code_system AS valueCodeableConcept_code_system,
        odvcc.display AS valueCodeableConcept_display,
        odda.code AS dataAbsentReason_code,
        odda.code_system AS dataAbsentReason_code_system,
        odda.display AS dataAbsentReason_display,
        o.referencerange,
        o.valueQuantity,
        o.subject.reference AS subject_ref,
        o.encounter.reference AS encounter_ref
    FROM observation AS o
    LEFT JOIN core__observation_dn_category AS odcat ON o.id = odcat.id
    LEFT JOIN core__observation_dn_code AS odc ON o.id = odc.id
    LEFT JOIN core__observation_dn_interpretation AS odi ON o.id = odi.id
    LEFT JOIN core__observation_dn_valuecodeableconcept AS odvcc ON o.id = odvcc.id
    LEFT JOIN core__observation_dn_dataabsentreason AS odda ON o.id = odda.id
)

SELECT
    id,
    category_code,
    category_code_system,
    status,
    observation_code,
    observation_code_system,
    interpretation_code,
    interpretation_code_system,
    interpretation_display,
    effectiveDateTime_day,
    effectiveDateTime_week,
    effectiveDateTime_month,
    effectiveDateTime_year,
    valueCodeableConcept_code,
    valueCodeableConcept_code_system,
    valueCodeableConcept_display,
    valueQuantity_value,
    valueQuantity_comparator,
    valueQuantity_unit,
    valueQuantity_system,
    valueQuantity_code,
    valueString,
    dataAbsentReason_code,
    dataAbsentReason_code_system,
    dataAbsentReason_display,
    subject_ref,
    encounter_ref,
    concat('Observation/', id) AS observation_ref
FROM temp_observation
WHERE
    effectiveDateTime_day BETWEEN date(
        from_iso8601_timestamp('2016-06-01')
    ) AND current_date;
