
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                -- select
                -- TOP SL, FIXDATE 18.05.2023, JIRA PROMEDWEB-148004
        
                distinct
            
                EPL.EvnPL_id as "EPL_ID",
                
                dbfpd.PrehospDirect_Code as "DIR_CODE",
                case
when dbfpd.PrehospDirect_Code = 1 then coalesce(NULLIF(RTRIM(dbflsd.LpuSection_Code), ''), '')
when dbfpd.PrehospDirect_Code = 2 then coalesce(NULLIF(RTRIM(CAST(dbfprehosplpu.Lpu_Ouz as varchar)), ''), '')
else '' end
                as "PDO_CODE",
                to_char(EPL.EvnPL_setDT, 'DD.MM.YYYY') as "SETDATE",
                to_char(EPL.EvnPL_setDT, 'HH24:MI:SS') as "SETTIME",
                to_char(EPL.EvnPL_disDT, 'DD.MM.YYYY') as "DISDATE",
                to_char(EPL.EvnPL_disDT, 'HH24:MI:SS') as "DISTIME",
                EPL.EvnPL_NumCard as "NUMCARD",
                CASE WHEN EPL.EvnPL_IsFirstTime = 2 then '1' else '0' END AS "VPERV",
                coalesce(EPL.EvnPL_Complexity, 0) as "KATEGOR",
                dbflpu.Lpu_OGRN as "OGRN",
                
PS.Person_SurName as "SURNAME",
PS.Person_FirName as "FIRNAME",
PS.Person_SecName as "SECNAME",
to_char(PS.Person_BirthDay, 'DD.MM.YYYY') as "BIRTHDAY",
CASE WHEN PS.Sex_id = 2 THEN '1' ELSE '0' END "POL_COD",
dbfss.SocStatus_code as "SOC_COD",
coalesce(dbfkls.Kladr_Code, dbfkla.Kladr_Code) as "KOD_TER",
PS.person_Snils as "SNILS",
                
                case when EPL.EvnPL_IsFinish = 2 then '1' else '0' end as "FINISH_ID",
                dbfrc.ResultClass_Code as "RSC_COD",
                dbfdiag.Diag_Code as "DZ_COD",
                dbfdiag.Diag_Name as "DZ_NAM",
                dbfdt.DeseaseType_code as "DST_COD",
                EPL.EvnPL_UKL as "UKL",
                coalesce(dbfdc.DirectClass_Code, 0) as "CODE_NAP",
                case
when dbfdc.DirectClass_Code = 1 then coalesce(NULLIF(RTRIM(dbflsdir.LpuSection_Code), ''), '')
when dbfdc.DirectClass_Code = 2 then coalesce(NULLIF(RTRIM(CAST(dbflpudir.Lpu_Ouz as varchar)), ''), '')
else '' end
                as "KUDA_NAP",
                dbfinv.PersonChild_IsInvalid_Code as "INVALID",
                dbfinv.PermRegion_Code as "REG_PERM",
                CASE WHEN PS.Server_pid = 0 THEN 'Да' ELSE 'Нет' END as "BDZ"
            
            -- end select
            from
                -- from
        
            v_EvnPL EPL
            
                    inner join v_EvnVizitPL EVizitPL on EVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and EVizitPL.Lpu_id = '901'
                    INNER JOIN v_PersonState PS ON PS.Person_id = EPL.Person_id
                
            inner join v_LpuSection LS on LS.LpuSection_id = EPL.LpuSection_id
            inner join LpuUnit LU on LU.LpuUnit_id = LS.LpuUnit_id
            
                    left join v_Polis pls on pls.Polis_id = ps.Polis_id
                
                        left join v_Diag as dbfdiag on dbfdiag.Diag_id = EPL.Diag_id
                        left join v_Lpu as dbflpu on dbflpu.Lpu_id = EPL.Lpu_id
                        left join ResultClass dbfrc on dbfrc.ResultClass_id = EPL.ResultClass_id
                        left join DeseaseType dbfdt on dbfdt.DeseaseType_id = EPL.DeseaseType_id
                        left join PrehospDirect dbfpd on dbfpd.PrehospDirect_id = EPL.PrehospDirect_id
                        left join v_Lpu dbfprehosplpu on dbfprehosplpu.Lpu_id = EPL.Lpu_did
                        left join LpuSection dbflsd on dbflsd.LpuSection_id = EPL.LpuSection_did
                        left join DirectClass dbfdc on dbfdc.DirectClass_id = EPL.DirectClass_id
                        left join v_Lpu dbflpudir on dbflpudir.Lpu_id = EPL.Lpu_oid
                        left join LpuSection dbflsdir on dbflsdir.LpuSection_id = EPL.LpuSection_oid
                        left join lateral(
                            select
                                CASE WHEN PersonChild.PersonChild_IsInvalid = 2 THEN '1' ELSE '0' END as PersonChild_IsInvalid_Code,
                                PersonSprTerrDop.PersonSprTerrDop_Code as PermRegion_Code
                            from
                                PersonChild
                                left join PersonSprTerrDop on PersonSprTerrDop.PersonSprTerrDop_id = PersonChild.PersonSprTerrDop_id
                            where PersonChild.Person_id = EPL.Person_id
                            order by PersonChild.PersonChild_insDT desc
                            limit 1
                        ) as dbfinv on true
                    
                            left join v_Polis pls2 on pls2.Polis_id = PS.Polis_id
                            left join SocStatus as dbfss on dbfss.SocStatus_id = PS.SocStatus_id
                            left join Address dbfaddr on dbfaddr.Address_id = coalesce(PS.PAddress_id, PS.UAddress_id)
                            left join KLStreet dbfkls on dbfkls.KLStreet_id = dbfaddr.KLStreet_id
                            left join KLArea dbfkla on dbfkla.KLArea_id = COALESCE(dbfaddr.KLTown_id, dbfaddr.KLCity_id, dbfaddr.KLSubRgn_id, dbfaddr.KLRgn_id) and dbfkls.KLStreet_id is null
                        
            left join lateral(
                select ServiceEvnStatus_id
                from v_ServiceEvnHist
                where Evn_id = EPL.EvnPL_id and ServiceEvnList_id = 1
                order by ServiceEvnHist_id desc
                limit 1
            ) as SEH1 on true
            left join v_ServiceEvnStatus SES1 on SES1.ServiceEvnStatus_id = SEH1.ServiceEvnStatus_id
        
                        -- end from
                    where
                        -- where
                        (1 = 1) and EPL.EvnClass_id = 3 and EPL.Lpu_id = '901' and EPL.EvnPL_setDt >= '2023-01-01'::timestamp and EPL.EvnPL_setDt < '2023-07-30'::timestamp + interval '1 day' and coalesce(dbfdiag.Diag_Code, '') not in('A53.0','A59.0','A54.0','R75','Z21','Z11.4','Z20.6','Z21','Z71.7','Z83.0','R75','F02.8','F02.4') and coalesce(dbfdiag.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(dbfdiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(dbfdiag.Diag_Code, '') not between 'F00.0' and 'F02.3' and coalesce(dbfdiag.Diag_Code, '') not between 'F03' and 'F99'
                        -- end where
                
                    order by
                        -- order by
                        EPL.EvnPL_id
                        -- end order by
                
