
            with vars as (select
                (select Person_id from v_EvnLabSample where EvnLabSample_id = 500801209378452 limit 1) as Person_id
            ),
            myvars as (
            	select localtimestamp as curdate
            )
            select
                ut.UslugaComplex_id as "UslugaComplex_id",
                ut.UslugaTest_ResultValue as "UslugaTest_ResultValue",
                ut.UslugaTest_ResultUnit as "UslugaTest_ResultUnit",
                cast(ut.UslugaTest_ResultLower as varchar) || ' - ' || cast(ut.UslugaTest_ResultUpper as varchar) as "UslugaTest_RefValues",
                cast(ut.UslugaTest_ResultLower as varchar) as "UslugaTest_ResultLower",
                cast(ut.UslugaTest_ResultUpper as varchar) as "UslugaTest_ResultUpper",
                ut.UslugaTest_ResultQualitativeText as "UslugaTest_ResultQualitativeText",
                to_char(ut.UslugaTest_CheckDT, 'DD.MM HH24:MI') as "UslugaTest_CheckDT",
                to_char(ut.UslugaTest_CheckDT, 'DD.MM.YYYY HH24:MI') as "UslugaTest_CheckDateTime",
                ut.UslugaTest_Comment as "UslugaTest_Comment",
                lpu.Lpu_Nick as "Lpu_Nick",
                MS.MedService_Name as "MedService_Name",
                (uc.UslugaComplex_Code || ' ' || coalesce(analyzertest.AnalyzerTest_SysNick, uc.UslugaComplex_Name)) as "UslugaComplex_Name",
                ut.UslugaTest_id as "UslugaTest_id",
                (select count(*) from v_UslugaTestHistory where UslugaTest_id = ut.UslugaTest_id) as "UslugaTestHistory_Count",
                coalesce(analyzertest.AnalyzerTest_SysNick, uc.UslugaComplex_Name) as "UslugaComplex_SysNick",
                analyzertest.AnalyzerTestType_id as "AnalyzerTestType_id"
            from v_UslugaTest ut
            inner join v_UslugaComplex uc on uc.UslugaComplex_id = ut.UslugaComplex_id
            inner join v_EvnLabSample els on els.EvnLabSample_id = ut.EvnLabSample_id
            left join v_MedService MS on MS.MedService_id = els.MedService_id
            inner join lateral(select lpu.Lpu_Nick from v_Lpu lpu where lpu.Lpu_id = ut.Lpu_id limit 1) lpu on true
                    left join lateral(
                select
                    at_child.AnalyzerTest_id,
                    at_child.AnalyzerTest_SysNick,
                    at_child.AnalyzerTestType_id
                from
                    lis.v_AnalyzerTest at_child
                    left join lis.v_AnalyzerTest at on at.AnalyzerTest_id = at_child.AnalyzerTest_pid
                    inner join v_UslugaComplexMedService ucms_at on at_child.UslugaComplexMedService_id = ucms_at.UslugaComplexMedService_id
                    left join v_UslugaComplex uctest on uctest.UslugaComplex_id = at_child.UslugaComplex_id
                where
                    ucms_at.UslugaComplex_id = ut.UslugaComplex_id
                    and at_child.analyzertest_pid is not null
                    and ucms_at.MedService_id = els.MedService_id
                    and (at.analyzertest_isnotactive is null or at.analyzertest_isnotactive = 1)
                    and (at.AnalyzerTest_endDT >= (select curdate from myvars) or at.AnalyzerTest_endDT is null)
                    and (at_child.AnalyzerTest_endDT >= (select curdate from myvars) or at_child.AnalyzerTest_endDT is null)
                    and (uctest.UslugaComplex_endDT >= (select curdate from myvars) or uctest.UslugaComplex_endDT is null)
                limit 1
            ) analyzertest on true
            where
                els.Person_id = (select Person_id from vars)
                and ut.UslugaTest_CheckDT is not NULL
                and uc.UslugaComplex_id in (3001035,3003744)
                
            order by ut.UslugaComplex_id ASC, ut.UslugaTest_CheckDT DESC
        
