
            with
                oldSample as
                    (select
                        t.PersonEvn_id,
                        t.MedService_id,
                        t.RefSample_id,
                        rs.RefMaterial_id,
                        lreq.PayType_id
                    from v_EvnLabSample t
                        inner join EvnLabRequest lreq on lreq.Evn_id = t.EvnLabRequest_id
                        left join v_RefSample rs on rs.RefSample_id = t.RefSample_id
                    where t.EvnLabSample_id = '180101080132362' limit 1),
                IsSeparateSample as
                    (select count(*) as count
                    from v_UslugaComplexMedService
                    where UslugaComplexMedService_IsSeparateSample = 2
                        and refsample_id = (select RefSample_id from oldSample)
                    )

            select
                substring(ELS.EvnLabSample_Num from 9 for 4) as "EvnLabSample_ShortNum",
                to_char (ELS.EvnLabSample_setDT, 'HH24:MI dd.mm.yyyy') as "EvnLabSample_setDT",
                ELS.EvnLabSample_id as "EvnLabSample_id"
            from v_EvnLabSample ELS
                inner join v_EvnLabRequest ELR on ELR.EvnLabRequest_id = ELS.EvnLabRequest_id
                left join v_RefSample rs on rs.RefSample_id = ELS.RefSample_id
            where
                ELS.PersonEvn_id = (select PersonEvn_id from oldSample)
                and ELS.MedService_id = (select MedService_id from oldSample)
                and rs.RefMaterial_id = (select RefMaterial_id from oldSample)
                and ELR.PayType_id = (select PayType_id from oldSample)
                and (select count from IsSeparateSample) = 0
                and ELR.SampleStatusType_id = 4 -- Новая проба взята, но не отправлена на анализатор
                and ELS.EvnLabSample_id != '180101080132362'
        
