
                select
                     E.EvnPL_id as "Evn_id"
                    ,E.EvnClass_id as "EvnClass_id"
                    ,E.Person_id as "Person_id"
                    ,E.PersonEvn_id as "PersonEvn_id"
                    ,E.Server_id as "Server_id"
                    ,PS.Person_SurName as "Person_SurName"
                    ,PS.Person_FirName as "Person_FirName"
                    ,PS.Person_SecName as "Person_SecName"
                    ,E.pmUser_insID as "pmUser_insID"
                    ,'ТАП' as "EvnClass_Name"
                    ,E.EvnPL_NumCard as "Evn_NumCard"
                    ,COALESCE(PS.Person_SurName, '') || ' ' || COALESCE(PS.Person_FirName, '') || ' ' || COALESCE(PS.Person_SecName, '') as "Person_FIO"
                    ,CASE WHEN PolT.PolisType_Code = 4 then '' ELSE COALESCE(RTRIM(Polis.Polis_Ser), '') END as "Polis_Ser"
                    ,CASE WHEN PolT.PolisType_Code = 4 then COALESCE(RTRIM(ps.Person_EdNum), '') ELSE COALESCE(RTRIM(Polis.Polis_Num), '') END as "Polis_Num"
                    ,COALESCE(RTRIM(PS.Person_Snils), '') as "Person_Snils"
                    ,to_char(PS.Person_BirthDay, 'DD.MM.YYYY') as "Person_BirthDay"
                    ,COALESCE(D.Diag_FullName, '') as "Diag_Name"
                    ,to_char(E.EvnPL_setDT, 'DD.MM.YYYY') as "Evn_setDate"
                    ,CASE WHEN E.EvnPL_IsFinish = 2 THEN to_char(E.EvnPL_disDT, 'DD.MM.YYYY') ELSE null END as "Evn_disDate"
                    ,RTRIM(LS.LpuSection_Name) as "LpuSection_Name"
                    ,RTRIM(MP.Person_Fio) as "MedPersonal_Fio"
                    ,'' as "PrehospType_Name"
                    ,'' as "EvnPS_HospCount"
                    ,'' as "LeaveType_Name"
                    ,PT.PayType_Name as "PayType_Name"
                    ,LB.LpuBuilding_Name as "LpuBuilding_Name"
                from
                    v_EvnPL E
                    inner join v_PersonState PS  on PS.Person_id = E.Person_id
                    left join v_EvnVizitPL EVPL  on EVPL.EvnVizitPL_pid = E.EvnPL_id
                        and EVPL.EvnVizitPL_Index = EVPL.EvnVizitPL_Count - 1
                    left join v_LpuSection LS  on LS.LpuSection_id = EVPL.LpuSection_id
                    left join v_LpuUnit LU  on LS.LpuUnit_id = LU.LpuUnit_id
                    left join v_LpuBuilding LB  on LU.LpuBuilding_id = LB.LpuBuilding_id
                    left join v_Diag D  on D.Diag_id = E.Diag_id
                    left join v_PersonPolis Polis  on Polis.Polis_id = PS.Polis_id
                    left join v_PolisType PolT  on PolT.PolisType_id = Polis.PolisType_id
                    left join PayType PT  on PT.PayType_id = EVPL.PayType_id
                    LEFT JOIN LATERAL (
                        select Person_Fio
                        from v_MedPersonal
                        where MedPersonal_id = EVPL.MedPersonal_id
                        LIMIT 1
                    ) MP on true
                where
                    (1 = 1) and E.Lpu_id = '1101011' and coalesce(D.Diag_Code, '') not between 'F00.0' and 'F99' and coalesce(D.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(D.Diag_Code, '') not between 'A50.0' and 'A64'
                     and E.EvnPL_setDate >= CAST('2022-02-13' as date) and E.EvnPL_setDate <= CAST('2022-02-13' as date)
             union all 
                select
                     E.EvnPS_id as "Evn_id"
                    ,E.EvnClass_id as "EvnClass_id"
                    ,E.Person_id as "Person_id"
                    ,E.PersonEvn_id as "PersonEvn_id"
                    ,E.Server_id as "Server_id"
                    ,PS.Person_SurName as "Person_SurName"
                    ,PS.Person_FirName as "Person_FirName"
                    ,PS.Person_SecName as "Person_SecName"
                    ,E.pmUser_insID as "pmUser_insID"
                    ,'КВС' as "EvnClass_Name"
                    ,E.EvnPS_NumCard as "Evn_NumCard"
                    ,COALESCE(PS.Person_SurName, '') || ' ' || COALESCE(PS.Person_FirName, '') || ' ' || COALESCE(PS.Person_SecName, '') as "Person_FIO"
                    ,CASE WHEN PolT.PolisType_Code = 4 then '' ELSE COALESCE(RTRIM(Polis.Polis_Ser), '') END as "Polis_Ser"
                    ,CASE WHEN PolT.PolisType_Code = 4 then COALESCE(RTRIM(ps.Person_EdNum), '') ELSE COALESCE(RTRIM(Polis.Polis_Num), '') END as "Polis_Num"
                    ,COALESCE(RTRIM(PS.Person_Snils), '') as "Person_Snils"
                    ,to_char(PS.Person_BirthDay, 'DD.MM.YYYY') as "Person_BirthDay"
                    ,COALESCE(D.Diag_FullName, '') as "Diag_Name"
                    ,to_char(E.EvnPS_setDT, 'DD.MM.YYYY') as "Evn_setDate"
                    ,to_char(COALESCE(E.EvnPS_disDT, E.EvnPS_OutcomeDT), 'DD.MM.YYYY') as "Evn_disDate"
                    ,RTRIM(LS.LpuSection_Name) as "LpuSection_Name"
                    ,RTRIM(MP.Person_Fio) as "MedPersonal_Fio"
                    ,PT.PrehospType_Name as "PrehospType_Name"
                    ,COALESCE(CAST(E.EvnPS_HospCount as varchar), '') as "EvnPS_HospCount"
                    ,LT.LeaveType_Name as "LeaveType_Name"
                    ,PayT.PayType_Name as "PayType_Name"
                    ,LB.LpuBuilding_Name as "LpuBuilding_Name"
                from
                    v_EvnPS E
                    inner join  v_PersonState PS on PS.Person_id = E.Person_id
                    left join v_EvnSection ES  on ES.EvnSection_pid = E.EvnPS_id
                        and ES.EvnSection_Index = ES.EvnSection_IndexMinusOne
                    left join v_LpuSection LS  on LS.LpuSection_id = ES.LpuSection_id
                    left join v_LpuUnit LU  on LS.LpuUnit_id = LU.LpuUnit_id
                    left join v_LpuBuilding LB  on LU.LpuBuilding_id = LB.LpuBuilding_id
                    left join v_Diag D  on D.Diag_id = E.Diag_id
                    left join v_PayType PayT  on PayT.PayType_id = COALESCE(ES.PayType_id, E.PayType_id)
                    left join v_PersonPolis Polis  on Polis.Polis_id = PS.Polis_id
                    left join v_PolisType PolT  on PolT.PolisType_id = Polis.PolisType_id
                    left join v_PrehospType PT  on PT.PrehospType_id = E.PrehospType_id
                    left join v_LeaveType LT  on LT.LeaveType_id = ES.LeaveType_id
                    LEFT JOIN LATERAL (
                        select Person_Fio
                        from v_MedPersonal
                        where MedPersonal_id = ES.MedPersonal_id
                        LIMIT 1
                    ) MP on true
                where
                    (1 = 1) and E.Lpu_id = '1101011' and coalesce(D.Diag_Code, '') not between 'F00.0' and 'F99' and coalesce(D.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(D.Diag_Code, '') not between 'A50.0' and 'A64'
                     and E.EvnPS_setDate >= CAST('2022-02-13' as date) and E.EvnPS_setDate <= CAST('2022-02-13' as date)
            
            order by
                 "Evn_setDate"
                ,"Evn_disDate"
                ,"Person_SurName"
                ,"Person_FirName"
                ,"Person_SecName"
                ,"Person_BirthDay"
        
