
            with myvar1 as (
                select
                    CASE
                        WHEN els.EvnLabSample_setDT is null then null
                        -- 5. Забракованные, если отмечен брак
                        WHEN els.DefectCauseType_id IS NOT NULL then 5
                        -- 4. Одобренные. Все результаты одобренны
                        WHEN EUP_EvnUslugaPar_id > 0 and NOTAPP_EvnUslugaPar_id = 0 then 4
                        -- 6. Частично одобренные. Есть одобренный результат и есть не одобренный результат
                        WHEN EUP_EvnUslugaPar_id > 0 then 6
                        -- 3. Выполненные. Получено с анализатора или заполнены хоть какие-то результаты
                        WHEN EUP_HAS_RESULT_EvnUslugaPar_id > 0 then 3
                        -- 2. В работе. Отправлено на анализатор
                        -- 8. В работе (Отсортировано). Пришло с анализатора
                        WHEN AN.Link_id IS NOT NULL then 2
                        -- 7. В работе (ручные методики)
                        WHEN a.Analyzer_Code = '000' and a.pmUser_insID = 1 and COALESCE(mstdid.MedServiceType_SysNick,'lab') <> 'pzm' then 7
                        -- 1. Новые
                        ELSE 1
                    END as LabSampleStatus_id
                from
                    v_EvnLabSample els
                    left join v_MedService msdid on msdid.MedService_id = els.MedService_did
                    left join v_MedServiceType mstdid on mstdid.MedServiceType_id = msdid.MedServiceType_id
                    left join lis.v_Analyzer a on a.Analyzer_id = els.Analyzer_id
                    left join lateral(
                        select
                            Link_id
                        from
                            lis.v_Link
                        where
                            link_object = 'EvnLabSample'
                            and object_id = els.EvnLabSample_id
                        limit 1
                    ) AN on true
                    left join lateral (
                        select
                            max(case when ut.UslugaTest_ResultApproved = 2 then 1 else 0 end) as EUP_EvnUslugaPar_id,
                            max(case when COALESCE(ut.UslugaTest_ResultApproved, 1) = 1 then 1 else 0 end) as NOTAPP_EvnUslugaPar_id,
                            max(case when ut.UslugaTest_ResultValue IS NOT NULL and ut.UslugaTest_ResultValue <> '' then 1 else 0 end) as EUP_HAS_RESULT_EvnUslugaPar_id
                        from v_UslugaTest ut
                        
                        where
                            ut.EvnLabSample_id = els.EvnLabSample_id
                            and ut.EvnDirection_id is null -- только тесты
                            
                    ) EUP on true
                where
                    els.EvnLabSample_id = '890101023213375'
                limit 1
            ),
            myvar2 as (
            	select
                	EvnLabSample_StudyDT
                from v_EvnLabSample
                where EvnLabSample_id = '890101023213375'
            ),
            myvar3 as (
            	select
                	CASE
                    	when (select LabSampleStatus_id from myvar1) in (1,2,7) then null
                    	when ((select LabSampleStatus_id from myvar1) in (3,4,6) and (select EvnLabSample_StudyDT from myvar2) is null) then dbo.tzgetdate()
                        	else (select EvnLabSample_StudyDT from myvar2)
                    end as EvnLabSample_StudyDT
            ),
            myvar4 as (
                select
                    CASE
                        when (select EvnLabSample_StudyDT from myvar2) is null
                        then (select EvnLabSample_StudyDT from myvar3) else (select EvnLabSample_StudyDT from myvar2)
                    end as EvnLabSample_StudyDT
            )

            update
                EvnLabSample
            set
                LabSampleStatus_id = (select LabSampleStatus_id from myvar1),
                EvnLabSample_StudyDT = (select EvnLabSample_StudyDT from myvar4)
            where
                Evn_id = '890101023213375'
        
