
            with lr as (
                select
                    EvnDirection_id,
                    EvnStatus_id,
                    EvnLabRequest_id,
                    EvnLabRequest_PrmTime,
                    EvnLabRequest_StatusDate
                from v_EvnLabRequest lr
                where (
                    lr.EvnLabRequest_StatusDate::date  between '2022-12-20' and '2023-07-19'
                    or lr.EvnLabRequest_PrmTime::date  between '2022-12-20' and '2023-07-19'
                )
                and (lr.MedService_id = '15853')

                
                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 '2022-12-20' and '2023-07-19'
                        or lr.EvnLabRequest_PrmTime::date  between '2022-12-20' and '2023-07-19'
                    )
                    
                    and lr.MedService_id in ('15853','14832','14933','14934','14936','14937','14938','14939','14940','14941','14942','14949','14951','14955','14966','15037','15688','15845','15847','15848','500801000000718','500801000000978','500801000001112','500801000001115','500801000001144','500801000001149','500801000001150','500801000001152','500801000001153','500801000001166','500801000001168','500801000001557','500801000001558','500801000001559','500801000001629','500801000001646','500801000001648','500801000001650','500801000001652','500801000001655','500801000001656','500801000001658','500801000001661','500801000001662','500801000001664','500801000001666','500801000001816','500801000001817','500801000002027','500801000002662','500801000002666','500801000002746','500801000002777','500801000002778','500801000002779','500801000002780','500801000002811','500801000002824','500801000002840','500801000002879','500801000002905','500801000002925','500801000002927','500801000002928','500801000002929','500801000002933','500801000002934','500801000003044','500801000003045','500801000003063','500801000003350','500801000003687','500801000003753','500801000003819','500801000003951','500801000004696','500801000004967','500801000004978','500801000004979','500801000004980','500801000004981','500801000004982','500801000004983','500801000004984','500801000004985','500801000004986','500801000004987','500801000004988','500801000004989','500801000004990','500801000005109','500801000005122','500801000005131','500801000005216','500801000005217','500801000005218','500801000005219','500801000005220','500801000005221','500801000005222','500801000005223','500801000005224','500801000005225','500801000005226','500801000005227','500801000005228','500801000005229','500801000005230','500801000005231','500801000005232','500801000005233','500801000005234','500801000005235','500801000005236','500801000005237','500801000005238','500801000005239','500801000005240','500801000005241','500801000005242','500801000005243','500801000005244','500801000005245','500801000005246','500801000005247','500801000005248','500801000005249','500801000005250','500801000005251','500801000005252','500801000005253','500801000005254','500801000005255','500801000005256','500801000005257','500801000005258','500801000005259','500801000005260','500801000005261','500801000005262','500801000005263','500801000005264','500801000005265','500801000005266','500801000005267','500801000005268','500801000005269','500801000005270','500801000005271','500801000005272','500801000005273','500801000005274','500801000005276','500801000005280','500801000005282','500801000005289','500801000005290','500801000005291','500801000005302','500801000005327','500801000005331','500801000005332','500801000005343','500801000005344','500801000005345','500801000005346','500801000005347','500801000005358','500801000005387','500801000005471','500801000005487','500801000005498','500801000005528','500801000005569','500801000005596','500801000006162','500801000006163','500801000006175','500801000006176','500801000006177','500801000006179','500801000006180','500801000006181','500801000006182','500801000006192','500801000006227','500801000006321','500801000006322','500801000006323','500801000006334','500801000006335','500801000006491','500801000007955','500801000007956','500801000008075','500801000008283','500801000008313','500801000008316','500801000008357','500801000008414','500801000008691','500801000008726','500801000008728','500801000008729','500801000008739','500801000009467')
                
                    and exists (
                        select 1 ex
                        from v_EvnLabSample els
                        where els.EvnLabRequest_id = lr.EvnLabRequest_id
                            and els.MedService_id = '15853'
                        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
                    
                where (1=1)
                    and ((lr.EvnStatus_id = 1 and (
                          (lr.EvnLabRequest_PrmTime is null and lr.EvnLabRequest_StatusDate::date  between '2022-12-20' and '2023-07-19') or
                          lr.EvnLabRequest_PrmTime::date  between '2022-12-20' and '2023-07-19'))
                          or (lr.EvnStatus_id > 1 and lr.EvnLabRequest_StatusDate::date  between '2022-12-20' and '2023-07-19'))
                    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))
                    
            ) A
            
            group by A.EvnStatus_id
        
