
            -- addit with
            with curDT as (select localtimestamp as curDate)
            -- end addit with

            select
                 count(  * ) AS cnt 
            from
                -- from
                v_EvnDirection_all ED
                inner join lateral (
                    select * from v_lpu where Lpu_id = ED.Lpu_sid
                    union
                    select * from v_lpu where Lpu_id = ED.Lpu_id and ED.Lpu_sid is null
                ) as lpu on true
                inner join v_DirType DT on DT.DirType_id = ED.DirType_id
                inner join lateral (
                    select
                        PS.Person_id,
                        PS.Sex_id,
                        dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT)) as age_newborn,
                        dbo.Age2(PS.Person_Birthday, (select curDate from curDT)) as age2,
                        datediff('day', PS.Person_Birthday, (select curDate from curDT)) as diff,
                        PS.Person_SurName,
                        PS.Person_SecName,
                        PS.Person_FirName,
                        to_char(PS.Person_Birthday, 'DD.MM.YYYY') as Person_Birthday
                    from v_PersonState PS
                    where ED.Person_id = PS.Person_id 
                    limit 1
                ) as PS on true
                LEFT JOIN LATERAL (
                    select
                        EQ.EvnQueue_id,
                        EQ.EvnQueue_setDT
                    from
                        v_EvnQueue EQ
                    where EQ.EvnDirection_id = ED.EvnDirection_id
                        and EQ.EvnQueue_recDT is null
                        and EQ.EvnQueue_failDT is null
                        and EQ.EvnQueue_IsArchived is null
                        limit 1
                ) EQ on true
                LEFT JOIN LATERAL (
                    select
                        Person_Fin,
                        Person_SurName,
                        Person_FirName,
                        Person_SecName
                    from v_MedPersonal
                    where MedPersonal_id = ED.MedPersonal_id
                        and Lpu_id = Lpu.Lpu_id
                    limit 1
                ) MP on true
                left join v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
                left join v_Diag Diag  on Diag.Diag_id = ED.Diag_id
                left join v_LpuSection LS  on ED.LpuSection_did = LS.LpuSection_id
                left join v_MedService MS on MS.MedService_id = ED.MedService_id and MS.Lpu_id = '144'
                left join v_MedServiceType MST on MST.MedServiceType_id = MS.MedServiceType_id
                left join v_TimetableStac_lite TTS on TTS.Evn_id = ED.EvnDirection_id
                left join lateral (
                    select
                        LeaveType_id,
                        PrehospWaifRefuseCause_id,
                        PrehospStatus_id,
                        EvnPS_setDate
                    from v_EvnPS
                    where EvnDirection_id = ED.EvnDirection_id
                    order by EvnPS_id desc
                    limit 1
                ) as EvnPS on true
                --left join v_EvnPS EvnPS on EvnPS.EvnDirection_id = ED.EvnDirection_id
                left join v_MedStaffFact MSF on MSF.MedStaffFact_id = ED.MedStaffFact_id
                left join v_LpuSectionProfile MedPersLSP on MedPersLSP.LpuSectionProfile_id = MSF.LpuSectionProfile_id
                left join v_LeaveType LT on EvnPS.LeaveType_id = LT.LeaveType_id
                left join v_PrehospWaifRefuseCause PWRC on PWRC.PrehospWaifRefuseCause_id = EvnPS.PrehospWaifRefuseCause_id
                left join v_DirFailType DFT on DFT.DirFailType_id = ED.DirFailType_id
                left join v_LpuSectionBedProfile LSBP on LSBP.LpuSectionBedProfile_id = ED.LpuSectionBedProfile_id
                LEFT JOIN LATERAL (
                    select EvnPrescrVK_id, Lpu_id
                    from v_EvnPrescrVK epvkHTM
                    where epvkHTM.EvnDirectionHTM_id = ED.EvnDirection_id
                    limit 1
                ) epvkHTM on true
                left join v_EvnDirectionHTM edh on edh.EvnDirectionHTM_id = ed.EvnDirection_id
                LEFT JOIN LATERAL (
                    select EvnPrescrVK_id, Lpu_id
                    from v_EvnPrescrVK epvkSM
                    where epvkSM.EvnDirection_sid = ED.EvnDirection_id
                        and epvkSM.Lpu_id = '144'
                    limit 1
                ) epvkSM on true
                left join (
                        select
                            COUNT(epvkHTM.EvnPrescrVK_id) as CountVK,
                            epvkHTM.EvnDirectionHTM_id as EvnDirectionHTM_id,
                            epvkHTM.Lpu_id as Lpu_id
                        from
		                    v_EvnPrescrVK epvkHTM
	                    group by epvkHTM.EvnDirectionHTM_id, epvkHTM.Lpu_id
                ) epvkHTMCount on
	                    ED.EvnDirection_id = epvkHTMCount.EvnDirectionHTM_id and epvkHTMCount.Lpu_id = '144'
                left join dbo.v_EvnVK evk
                    on evk.EvnPrescrVK_id = coalesce((case when epvkHTM.Lpu_id = '144' then epvkHTM.EvnPrescrVK_id else NULL end), epvkSM.EvnPrescrVK_id)
                left join dbo.v_EvnLink el on el.Evn_id = coalesce((case when epvkHTM.Lpu_id = '144' then epvkHTM.EvnPrescrVK_id else NULL end), epvkSM.EvnPrescrVK_id)
                left join dbo.v_EvnDirection_all evkd on evkd.EvnDirection_id = el.Evn_lid
                left join v_TalonHTM thtm on thtm.EvnDirectionHTM_id = ed.EvnDirection_id
                
                left join Evn ParentEvn on ParentEvn.Evn_id = ED.EvnDirection_pid and ParentEvn.Evn_deleted = 1
                left join EvnClass ParentEvnClass on ParentEvnClass.EvnClass_id = ParentEvn.EvnClass_id
                left join Evn RootEvn on RootEvn.Evn_id = ED.EvnDirection_rid and RootEvn.Evn_deleted = 1
                left join EvnClass RootEvnClass on RootEvnClass.EvnClass_id = RootEvn.EvnClass_id
            
                
                -- end from
            where
                -- where
                ED.EvnDirection_setDT < (cast('2023-07-23' as date) + '1 day'::interval)
                and ED.EvnDirection_setDT >= cast('2022-11-23' as date)
                
                and coalesce(ED.EvnStatus_id, 0) not in (12,13) and ED.EvnDirection_failDT is null and coalesce(ED.EvnDirection_IsConfirmed, 1) = '1' and coalesce(Diag.Diag_Code, '') not in('Z20.6','R75','Z11.4','Z71.7','Z83.0','Z21') and coalesce(Diag.Diag_Code, '') not between 'F10.0' and 'F19.9' and coalesce(Diag.Diag_Code, '') not between 'F00.0' and 'F09' and coalesce(Diag.Diag_Code, '') not between 'F20.0' and 'F99' and coalesce(Diag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(Diag.Diag_Code, '') not between 'A50.0' and 'A64' and (ED.Lpu_did = '144' or ED.Org_oid = '144')
                
                
                and (
                    DT.DirType_Code in (1,4,5)
                    or (
                        DT.DirType_Code = 6
                        and MST.MedServiceType_SysNick = 'osmotrgosp'
                        and exists (
                            select MedServiceMedPersonal_id
                            from v_MedServiceMedPersonal
                            where MedPersonal_id = '560101000071990'
                                and (MedServiceMedPersonal_begDT is null or MedServiceMedPersonal_begDT <= (select curDate from curDT))
                                and (MedServiceMedPersonal_endDT is null or MedServiceMedPersonal_endDT >= (select curDate from curDT))
                            limit 1
                        )
                    )
                    or (
                    DT.DirType_Code in ('15')
                    and ED.LpuSectionProfile_id in (
                        select LpuSectionProfile_id
                        from v_LpuSectionLpuSectionProfile
                        where LpuSection_id = '560101000001312'

                        union all

                        select LpuSectionProfile_id
                        from v_LpuSection
                        where LpuSection_id = '560101000001312'
                    )
                    and ED.EvnStatus_id in (14, 16, 36)
            )
                )
            
                -- end where
            
        
