
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                 count(  * ) AS cnt 
            from
                -- from
         dbo.v_PersonState PS 

-- person inner join placeholder --

 inner join v_EvnPS EPS on EPS.Person_id = PS.Person_id
                            and EPS.Lpu_id = '13100932'
            left join v_EvnCostPrint ecp on ecp.Evn_id = EPS.EvnPS_id
            left join v_EvnSection EPSLastES on EPSLastES.EvnSection_id = EPS.EvnSection_lastid
                and EPSLastES.EvnSection_pid = EPS.EvnPS_id
            left join LpuSection LStmp on LStmp.LpuSection_id = EPSLastES.LpuSection_id
            left join LpuSectionProfile LSP on LSP.LpuSectionProfile_id = coalesce(EPSLastES.LpuSectionProfile_id, LStmp.LpuSectionProfile_id)
            left join v_Diag Dtmp on Dtmp.Diag_id = EPSLastES.Diag_id
            left join v_Diag eps_diag on EPS.Diag_id = eps_diag.Diag_id
            left join v_Diag eps_diag_depart on EPS.Diag_did = eps_diag_depart.Diag_id
            left join LeaveType LT on LT.LeaveType_id = coalesce(EPSLastES.LeaveType_id, EPSLastES.LeaveType_prmid)
            left join v_Diag DP on DP.Diag_id = EPS.Diag_pid
            left join v_PrehospWaifRefuseCause pwrc on pwrc.PrehospWaifRefuseCause_id = EPS.PrehospWaifRefuseCause_id
            left join LpuUnit on LpuUnit.LpuUnit_id = LStmp.LpuUnit_id
            left join LpuUnitType on LpuUnitType.LpuUnitType_id = LpuUnit.LpuUnitType_id
            left join PayType dbfpayt on dbfpayt.PayType_id = EPS.PayType_id
            left join v_Polis pls on pls.Polis_id = ps.Polis_id
            left join lateral(
                select EvnDie_setDate
                from v_EvnDie
                where Person_id = PS.Person_id
                order by EvnDie_setDt
                limit 1
            ) as EvnDie on true
            left join lateral(
                select
                    DeathSvid_id,
                    DeathSvid_DeathDate
                from dbo.v_DeathSvid
                where Person_id = PS.Person_id
                    and (DeathSvid_IsBad is null or DeathSvid_IsBad = 1)
                    and DeathSvid_IsActual = 2
                limit 1
            ) as DeathSvid on true
            left join v_MesTariff spmt on EPSLastES.MesTariff_id = spmt.MesTariff_id
            left join v_MesOld as sksg on sksg.Mes_id = EPSLastES.Mes_sid
            
            left join v_MesOld as ksgkpg on spmt.Mes_id = ksgkpg.Mes_id
            left join v_MesOld as kpg on kpg.Mes_id = EPSLastES.Mes_kid
            
        
                        left join v_EvnLeave EL on EL.EvnLeave_pid = EPSLastES.EvnSection_id and LT.LeaveType_SysNick in ('leave', 'ksleave', 'dsleave', 'inicpac', 'ksinicpac', 'ksiniclpu', 'iniclpu', 'ksprerv', 'prerv', 'ksprod') and EL.Lpu_id = '13100932'
                        left join v_EvnOtherLpu EOL on EOL.EvnOtherLpu_pid = EPSLastES.EvnSection_id and LT.LeaveType_SysNick in ('other', 'dsother', 'ksother', 'ksperitar') and EOL.Lpu_id = '13100932'
                        left join v_EvnDie ED on ED.EvnDie_pid = EPSLastES.EvnSection_id and LT.LeaveType_SysNick in ('die', 'ksdie', 'ksdiepp', 'diepp', 'dsdie', 'dsdiepp', 'kslet', 'ksletitar') and ED.Lpu_id = '13100932'
                        left join v_EvnOtherStac EOST on EOST.EvnOtherStac_pid = EPSLastES.EvnSection_id and LT.LeaveType_SysNick in ('stac', 'ksstac', 'dsstac') and EOST.Lpu_id = '13100932'
                        left join v_EvnOtherSection EOS on EOS.EvnOtherSection_pid = EPSLastES.EvnSection_id and LT.LeaveType_SysNick in ('section', 'dstac', 'kstac') and EOS.Lpu_id = '13100932'
                        left join v_EvnOtherSectionBedProfile EOSBP on EOSBP.EvnOtherSectionBedProfile_pid = EPSLastES.EvnSection_id and LT.LeaveType_SysNick in ('ksper', 'dsper') and EOSBP.Lpu_id = '13100932'
                    
            left join lateral(
                select ServiceEvnStatus_id
                from v_ServiceEvnHist
                where Evn_id = EPS.EvnPS_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 EOST.LpuUnitType_oid = '6' and coalesce(EOS.LpuSection_oid, EOST.LpuSection_oid, EOSBP.LpuSection_oid) = '99560024122'
                        -- end where
                
                        
                    
