
            with lr as (
                select
                    EvnDirection_id,
                    EvnStatus_id,
                    EvnLabRequest_id,
                    EvnLabRequest_PrmTime,
                    EvnLabRequest_StatusDate
                from v_EvnLabRequest lr
                where (
                    lr.EvnLabRequest_StatusDate::date  between '2023-01-02' and '2023-07-24'
                    or lr.EvnLabRequest_PrmTime::date  between '2023-01-02' and '2023-07-24'
                )
                and (lr.MedService_id = '16016')

                
                union
                select
                    EvnDirection_id,
                    EvnStatus_id,
                    EvnLabRequest_id,
                    EvnLabRequest_PrmTime,
                    EvnLabRequest_StatusDate
                from v_EvnLabRequest lr
                where (1=1)
                    and (
                        lr.EvnLabRequest_StatusDate::date  between '2023-01-02' and '2023-07-24'
                        or lr.EvnLabRequest_PrmTime::date  between '2023-01-02' and '2023-07-24'
                    )
                    
                    and lr.MedService_id in ('16016','500801000001004','500801000001006','500801000001007','500801000001008','500801000001011','500801000001013','500801000001014','500801000001015','500801000001016','500801000001017','500801000001018','500801000001022','500801000001023','500801000001024','500801000001025','500801000001027','500801000001031','500801000001032','500801000001033','500801000001034','500801000001039','500801000001042','500801000001044','500801000001045','500801000001046','500801000001048','500801000001771','500801000005662','500801000008682','500801000009554')
                
                    and exists (
                        select 1 ex
                        from v_EvnLabSample els
                        where els.EvnLabRequest_id = lr.EvnLabRequest_id
                            and els.MedService_id = '16016'
                        limit 1
                    )
            
            )
            select
                A.EvnStatus_id as "EvnStatus_id",
                count(A.EvnDirection_id) as "count"
            from (
                select distinct
                    lr.EvnDirection_id as EvnDirection_id,
                    lr.EvnStatus_id as EvnStatus_id
                from lr
                    inner join v_EvnDirection_all ed on lr.EvnDirection_id = ed.EvnDirection_id
                    left join v_EvnUslugaPar EvnUsluga on ed.EvnDirection_id = EvnUsluga.EvnDirection_id
                where (1=1)
                    and ((lr.EvnStatus_id = 1 and (
                          (lr.EvnLabRequest_PrmTime is null and lr.EvnLabRequest_StatusDate::date  between '2023-01-02' and '2023-07-24') or
                          lr.EvnLabRequest_PrmTime::date  between '2023-01-02' and '2023-07-24'))
                          or (lr.EvnStatus_id > 1 and lr.EvnLabRequest_StatusDate::date  between '2023-01-02' and '2023-07-24'))
                    and ed.dirtype_id in (10, 25, 30)
                    and ed.DirFailType_id is null
                    and (lr.EvnStatus_id IN (3,4) OR COALESCE(ED.EvnStatus_id, 1) not in (12,13))
                    and EvnUsluga.UslugaComplex_id = '206842'
            ) A
            
            group by A.EvnStatus_id
        
