-- noqa: disable=all

-- This template is provided as an example only of queries generated by PSM table creation.
-- It is not used in actual validation
CREATE TABLE "cumulus"."core__psm_cohort_sampled_ids" AS (
    SELECT * FROM (
        VALUES
        ((cast('Patient/0b9f1de5dafc274110f78ee28f5eebd446b5b89f9f5ff18adf964694f6f7f8de' AS varchar),cast('Encounter/67f36354731b7591109ba73148a68354d717592e6a790310584c0579571c2d28' AS varchar),cast('1' AS varchar))),
        ((cast('Patient/a50737315a9b5d74573be547a93de304072070fd9851da0429c9eb68398eebf4' AS varchar),cast('Encounter/445125bad3dadc0668513c26f029cd122ac923de5e1aa107887dec4f2b8f47de' AS varchar),cast('1' AS varchar))),
        ((cast('Patient/a82d6443f6ef1e64b7542fd8d6bd8ae6fa08f525abfb2d6c212ce3a51883d8a7' AS varchar),cast('Encounter/b29ea4400966b83f380ecb8129e9939e62bcacc506b323677d70283a16ea5704' AS varchar),cast('1' AS varchar))),
        ((cast('Patient/3aafe05267ec191199c697a4b9c0140f11d567ca1edcd97a6a91e9e35fa6d99e' AS varchar),cast('Encounter/439122de45e8b2d124d65a88a525903ba4837dc980e6b409f866dd1eaec7962f' AS varchar),cast('1' AS varchar))),
        ((cast('Patient/0efd7a0cd26cae01e13fba909bcaafd445a2196117f4393c3b5f3c64245ef4c3' AS varchar),cast('Encounter/fc14df1992d4f7586914819bca5065c5b90d544e8c989be838ad8273a0bdb729' AS varchar),cast('1' AS varchar))),
        ((cast('Patient/564a3c168f33783e76ce60b1dcf05f1eb9857c1d2459e729819ed45174bfa1c1' AS varchar),cast('Encounter/a2ed39553b6014205d3199dc9968f34d4ad2318d34dbb413475e7c430feeabf1' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/7229f84324dc9eb4e92ac1c483f7307d984a9e2ab9f4d8db098c919629be4f2f' AS varchar),cast('Encounter/6b2f6ef2495d772416abd254504db3fa1c8f2ac13c50f926c25ba280f1b4924d' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/fd32b483921f8c16d0127e9c7f5af3112a973d304fc1ce335b70e439741c8004' AS varchar),cast('Encounter/4cab98a3f5c6ba9439285e19a6ecba6bc50083f613700f992511dc57a572ddcb' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/0b8fed84ab3969013e735852e69d7666c805b11d31200bbab206c3ebf8392e17' AS varchar),cast('Encounter/1e861fb766e92242a3662dc13d885b394d64eeb7ca58a17ceb1129cc49ee9e3f' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/19c65c23167b931cf07f550a8fd3fdb67d634b20e319ce2aa0eab9f22c6ecc3e' AS varchar),cast('Encounter/5e7aa658227c5d4fa139e8382de7a31c31155ca6d8d0a90d5b1580406d23700f' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/de465cd0da7ed926ec5995d7936e825e5510d718624c2cbf9a09432d8c3a1c99' AS varchar),cast('Encounter/a56cd00ceeb6b320ec0f2bb901dd1219e4d909c554e568da4fd5fb8e24b85c1d' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/e3a532c179a12902a2439df569e93e8b48ebed09e84e7a433ff61935f19eff8f' AS varchar),cast('Encounter/7d115ba035c032f49987915075dc007b11253603fa3f898a78013ec66d5bb2a8' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/844fca509614791cc8b9d4fe42dc234599a408bc128d65892a77c550d2c2da8c' AS varchar),cast('Encounter/28a8a7eab0e0585d30b5ce0db87ad022275e44c088e1e301aebe04fab67248e7' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/34cb4e36a97cf468ae7fe1470a66e27613be69a27dbf45e6239b4c3d0573e340' AS varchar),cast('Encounter/633674cb8cc4efe4fd7e9b6ef5985d8c701dd3d37e0b183ba8d2f62aea2ccf3f' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/43e51cdf15d8379e6eae07a0d0e410524dfc2d8546ba214c7c73ecd3ea51011e' AS varchar),cast('Encounter/2fc7a4a9be2f9df9053da6b2f91e63324c8287ee01fff9fb543036a288deca4d' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/3e8dd084158f78dffef04f0a393a8b1ce8710d6345eaeea04d344279076af8f3' AS varchar),cast('Encounter/448f1b4fded6f62e8725dc9f40f6a8a665ba605cdd117b34b0f7fa159f8daf20' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/890668e1eed6daec60fb134725d58b414f4269d829adbf71baf1fe208019955a' AS varchar),cast('Encounter/e643b07cc8ff6979e946ce47b170bae301a4f78fd8bf1b375073edb3ee05b1c7' AS varchar),cast('0' AS varchar))),
        ((cast('Patient/276e068ce8463e6dd018eca0d2684c928fe69b1a191d966ff3c70b10244310e0' AS varchar),cast('Encounter/515b234f80eb953c2c1ca7629008a9404a5e9b984f7d04e2fbc8c5c89f41193d' AS varchar),cast('0' AS varchar)))
    )
        AS t
        ("subject_ref","encounter_ref","example_diagnosis")
);

-- ###########################################################
CREATE TABLE core__psm_encounter_covariate AS (
    SELECT 
        DISTINCT sample_cohort."encounter_ref",
        sample_cohort."example_diagnosis",
        (
            SELECT COUNT( DISTINCT encounter_ref )
            FROM "core__condition"
            WHERE sample_cohort."subject_ref" = "core__condition"."subject_ref"
        ) AS instance_count,
        "core__encounter"."gender",
        "core__encounter"."race_display" AS "race",
        core__condition.code
    FROM "core__psm_cohort_sampled_ids" AS sample_cohort, 
        "core__condition",
        "core__encounter"
    WHERE sample_cohort."encounter_ref" = "core__condition"."encounter_ref"
        AND sample_cohort."encounter_ref" = "core__encounter"."encounter_ref"
    ORDER BY sample_cohort."encounter_ref"
)
