
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                -- select
                -- TOP SL, FIXDATE 18.05.2023, JIRA PROMEDWEB-148004
        
                distinct
            
            PS.PersonEvn_id as "PCT_ID",
            PS.Person_id as "P_ID",
            PS.Person_SurName as "SURNAME",
            PS.Person_FirName as "FIRNAME",
            PS.Person_SecName as "SECNAME",
            trim(coalesce(to_char(PS.Person_BirthDay, 'DD.MM.YYYY'), '')) as "BIRTHDAY",
            PS.Person_Snils as "SNILS",
            PrsIsInv.YesNo_Code as "INV_N",
            PrsInvD.Diag_Code as "INV_DZ",
            trim(coalesce(to_char(PrsPCh.PersonChild_invDate, 'DD.MM.YYYY'), '')) as "INV_DATA",
            PrsSex.Sex_Name as "SEX",
            PrsSoc.SocStatus_Name as "SOC",
            PrsOMSST.OMSSprTerr_Code as "P_TERK",
            PrsOMSST.OMSSprTerr_Name as "P_TER",
            PrsPolTp.PolisType_Name as "P_NAME",
            PrsPol.Polis_Ser as "P_SER",
            PrsPol.Polis_Num as "P_NUM",
            PS.Person_EdNum as "P_NUMED",
            trim(coalesce(to_char(PrsPol.Polis_begDate, 'DD.MM.YYYY'), '')) as "P_DATA",
            PrsOSO.Org_Code as "SMOK",
            PrsOS.OrgSmo_Name as "SMO",
            0 as "AR_TP",
            PrsUA.Address_Zip as "AR_IDX",
            PrsUA.KLCountry_Name as "AR_LND",
            PrsUA.KLRGN_Name as "AR_RGN",
            PrsUA.KLSubRGN_Name as "AR_RN",
            PrsUA.KLCity_Name as "AR_CTY",
            PrsUA.KLTown_Name as "AR_NP",
            PrsUA.KLStreet_Name as "AR_STR",
            PrsUA.Address_House as "AR_DOM",
            PrsUA.Address_Corpus as "AR_K",
            PrsUA.Address_Flat as "AR_KV",
            0 as "AP_TP",
            PrsPA.Address_Zip as "AP_IDX",
            PrsPA.KLCountry_Name as "AP_LND",
            PrsPA.KLRGN_Name as "AP_RGN",
            PrsPA.KLSubRGN_Name as "AP_RN",
            PrsPA.KLCity_Name as "AP_CTY",
            PrsPA.KLTown_Name as "AP_NP",
            PrsPA.KLStreet_Name as "AP_STR",
            PrsPA.Address_House as "AP_DOM",
            PrsPA.Address_Corpus as "AP_K",
            PrsPA.Address_Flat as "AP_KV",
            PrsDocTp.DocumentType_Name as "D_TIP",
            PrsDoc.Document_Ser as "D_SER",
            PrsDoc.Document_Num as "D_NOM",
            PrsOrgD.Org_id as "D_OUT",
            trim(coalesce(to_char(PrsDoc.Document_begDate, 'DD.MM.YYYY'), '')) as "D_DATA",
         
	
                EPLS.EvnPLStom_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(EPLS.EvnPLStom_setDate, 'DD.MM.YYYY') as "SETDATE",
                to_char(EPLS.EvnPLStom_setDate, 'HH24:MI:SS') as "SETTIME",
                to_char(EPLS.EvnPLStom_disDate, 'DD.MM.YYYY') as "DISDATE",
                to_char(EPLS.EvnPLStom_disDate, 'HH24:MI:SS') as "DISTIME",
                RTRIM(EPLS.EvnPLStom_NumCard) as "NUMCARD",
                dbfift.YesNo_Code as "VPERV",
                coalesce(EPLS.EvnPLStom_Complexity, 0) as "KATEGOR",
                dbflpu.Lpu_OGRN as "OGRN",
                case when EPLS.EvnPLStom_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",
                cast(EPLS.EvnPLStom_UKL as float) 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
         dbo.v_PersonState PS inner join v_EvnPLStom EPLS on EPLS.Person_id = PS.Person_id
                        left join v_Diag as dbfdiag on dbfdiag.Diag_id = EPLS.Diag_id
                        left join v_Lpu as dbflpu on dbflpu.Lpu_id = EPLS.Lpu_id
                        left join ResultClass dbfrc on dbfrc.ResultClass_id = EPLS.ResultClass_id
                        left join DeseaseType dbfdt on dbfdt.DeseaseType_id = EPLS.DeseaseType_id
                        left join PrehospDirect dbfpd on dbfpd.PrehospDirect_id = EPLS.PrehospDirect_id
                        left join v_Lpu dbfprehosplpu on dbfprehosplpu.Lpu_id = EPLS.Lpu_did
                        left join LpuSection dbflsd on dbflsd.LpuSection_id = EPLS.LpuSection_did
                        left join YesNo dbfift on dbfift.YesNo_id = EPLS.EvnPLStom_IsFirstTime
                        left join DirectClass dbfdc on dbfdc.DirectClass_id = EPLS.DirectClass_id
                        left join v_Lpu dbflpudir on dbflpudir.Lpu_id = EPLS.Lpu_oid
                        left join LpuSection dbflsdir on dbflsdir.LpuSection_id = EPLS.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 = EPLS.Person_id
                            order by PersonChild.PersonChild_insDT desc
                            limit 1
                        ) dbfinv on true
                    
                                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 Sex PrsSex on PrsSex.Sex_id = PS.Sex_id
                    left join SocStatus PrsSoc on PrsSoc.SocStatus_id = PS.SocStatus_id
                    left join PersonChild PrsPCh on PrsPCh.Person_id = PS.Person_id
                    left join YesNo PrsIsInv on PrsIsInv.YesNo_id = PrsPCh.PersonChild_IsInvalid
                    left join Diag PrsInvD on PrsInvD.Diag_id = PrsPCh.Diag_id
                    left join Polis PrsPol on PrsPol.Polis_id = PS.Polis_id
                    left join OMSSprTerr PrsOMSST on PrsOMSST.OMSSprTerr_id = PrsPol.OMSSprTerr_id
                    left join PolisType PrsPolTp on PrsPolTp.PolisType_id = PrsPol.PolisType_id
                    left join v_OrgSmo PrsOS on PrsOS.OrgSmo_id = PrsPol.OrgSmo_id
                    left join v_Org PrsOSO on PrsOSO.Org_id = PrsOS.Org_id
                    left join v_Address_all PrsUA on PrsUA.Address_id = PS.UAddress_id
                    left join v_Address_all PrsPA on PrsPA.Address_id = PS.PAddress_id
                    left join Document PrsDoc on PrsDoc.Document_id = PS.Document_id
                    left join DocumentType PrsDocTp on PrsDocTp.DocumentType_id = PrsDoc.DocumentType_id
                    left join v_OrgDep PrsOrgD on PrsOrgD.OrgDep_id = PrsDoc.OrgDep_id
                
            left join lateral(
                select *
                from v_MedPersonal
                where MedPersonal_id = EPLS.MedPersonal_id
                  and Lpu_id = '13101851'
                order by case when Lpu_id = '13101851' then 1 else 2 end
                limit 1
            ) as MP on true
        
                        -- end from
                    where
                        -- where
                        (1 = 1) and coalesce(dbfdiag.Diag_Code, '') not in('B18.0','B18.1','B17.1','B18.2','Z11.3','Z03.2','Z72.2') and coalesce(dbfdiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(dbfdiag.Diag_Code, '') not between 'A50.0' and 'A64' and coalesce(dbfdiag.Diag_Code, '') not between 'B16.0' and 'B16.9' and coalesce(dbfdiag.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(dbfdiag.Diag_Code, '') not between 'F00.0' and 'F09' and coalesce(dbfdiag.Diag_Code, '') not between 'F20.0' and 'F99' and coalesce(dbfdiag.Diag_Code, '') not between 'Z50.2' and 'Z50.3' and coalesce(dbfdiag.Diag_Code, '') not between 'F10.0' and 'F19.9'
                        -- end where
                
                    order by
                        -- order by
                        EPLS.EvnPLStom_id
                        -- end order by
                
