
            --variables
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            --end variables
        
            select
                 count(  * ) AS cnt 
            from
                -- from
         dbo.v_PersonState PS  inner join v_EvnPS EPS on EPS.Person_id = PS.Person_id
                            and EPS.Lpu_id = '63'
                inner join v_EvnSection firstProfileES on firstProfileES.EvnSection_id = EPS.EvnSection_firstid
                    and firstProfileES.EvnSection_pid = EPS.EvnPS_id
            
                left join lateral(
                    select
                        ESS.Evn_id AS EvnSection_id
                    from EvnSection ESS
                        inner join Diag ESSDiag on ESSDiag.Diag_id = ESS.Diag_id
                        where ESS.Evn_pid = EPS.EvnPS_id
                        and ESS.Evn_Index = ESS.Evn_Count - 1
                        and ESS.Lpu_id = '63'
                        and ((ESSDiag.Diag_Code between 'A15.0' and 'A19.9') or (ESSDiag.Diag_Code between 'B20.0' and 'B24') or (ESSDiag.Diag_Code between 'F00.0' and 'F99.1'))
                        and ESS.Evn_id is null
                    limit 1
                ) adf on true
            
            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_setDate
                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 lateral(
                select LU_tmp.LpuUnitType_id
                from
                    v_EvnSection ES_tmp
                    inner join LpuSection LS_tmp on LS_tmp.LpuSection_id = ES_tmp.LpuSection_id
                    inner join LpuUnit LU_tmp on LU_tmp.LpuUnit_id = LS_tmp.LpuUnit_id
                where ES_tmp.EvnSection_rid = EPS.EvnPS_id
                  and ES_tmp.Lpu_id = '63'
                  and coalesce(ES_tmp.EvnSection_IsPriem, 1) = 1
                order by ES_tmp.EvnSection_setDT
                limit 1
            ) as ESHosp on true
        
            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 EPSLastES.EvnSection_disDate >= '2022-01-01' and EPSLastES.EvnSection_disDate <= '2022-12-31' and firstProfileES.LpuSection_id = '10101000001433' and ESHosp.LpuUnitType_id = '1'
                        and EPS.EvnPS_id = any(array(
                            select ES.EvnSection_rid
                            from v_EvnSection ES
                            where ES.Lpu_id = '63'

                     and ES.LpuSection_id = '10101000001433')) and coalesce(Dtmp.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(Dtmp.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(Dtmp.Diag_Code, '') not between 'F00.0' and 'F99.1' and coalesce(eps_diag.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(eps_diag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(eps_diag.Diag_Code, '') not between 'F00.0' and 'F99.1' and coalesce(eps_diag_depart.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(eps_diag_depart.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(eps_diag_depart.Diag_Code, '') not between 'F00.0' and 'F99.1' and coalesce(DP.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(DP.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(DP.Diag_Code, '') not between 'F00.0' and 'F99.1'
                        -- end where
                
                        
                    
