
            with myvars as (
                select to_timestamp('2022-03-28', 'YYYY.MM.DD HH24:MI:SS') as paramDate
            )

            SELECT DISTINCT
                ut.UslugaComplex_id as "UslugaComplex_id",
                at.AnalyzerTest_SysNick as "AnalyzerTest_SysNick",
                at.AnalyzerTest_id as "AnalyzerTest_id",
                at.AnalyzerTestType_id as "AnalyzerTestType_id",
                uc.UslugaComplex_Name as "UslugaComplex_Name",
                ucgost.UslugaComplex_Name as "UslugaComplex_Name_Gost",
                coalesce(at.AnalyzerTest_SysNick, uc.UslugaComplex_Name, ucgost.UslugaComplex_Name,'NoName') as "TestName",
                COALESCE(at.AnalyzerTest_SortCode, '777') as "AnalyzerTest_SortCode",
                at2.UslugaComplex_id as "UslugaComplex_pid"
            FROM
                dbo.v_EvnLabSample els
                INNER JOIN dbo.v_UslugaTest ut ON ut.EvnLabSample_id = els.EvnLabSample_id
                LEFT JOIN dbo.MedServiceLink msl ON msl.MedService_id = '500801000001870'
                inner join lateral (
                    select distinct
                        ucms.UslugaComplexMedService_id
                    from v_EvnLabRequestUslugaComplex elruc
                        inner join v_EvnUslugaPar ep on elruc.EvnUslugaPar_id = ep.EvnUslugaPar_id
                        inner join v_UslugaComplexMedService ucms on ucms.UslugaComplex_id = ep.UslugaComplex_id
                            and ucms.MedService_id = msl.MedService_lid
                            and ucms.UslugaComplexMedService_pid is null
                    where ut.EvnLabSample_id = elruc.EvnLabSample_id
                        and ut.UslugaComplex_id = elruc.UslugaComplex_id
                ) pucms on true
                inner join v_UslugaComplexMedService ucms on pucms.UslugaComplexMedService_id = ucms.UslugaComplexMedService_pid
                    and ucms.UslugaComplex_id = ut.UslugaComplex_id
                inner JOIN lis.v_AnalyzerTest at ON at.UslugaComplexMedService_id = ucms.UslugaComplexMedService_id
                inner JOIN dbo.v_UslugaComplex uc ON uc.UslugaComplex_id = at.UslugaComplex_id
                LEFT JOIN dbo.v_UslugaComplex ucgost ON uc.UslugaComplex_2011id = ucgost.UslugaComplex_id
                LEFT JOIN lis.AnalyzerTest at2 ON at2.AnalyzerTest_id = at.AnalyzerTest_pid and ucms.MedService_id = msl.MedService_lid
            WHERE
                at.AnalyzerTest_pid is not null
                
                AND els.MedService_id IN (
            select MSL.MedService_lid from MedServiceLink MSL where msl.MedService_id = '500801000001870'
        )
                AND els.EvnLabSample_setDT >= (select paramDate from myvars)
                AND els.EvnLabSample_setDT < INTERVAL '1 day' + (select paramDate from myvars)
            ORDER BY
                "AnalyzerTest_SortCode",
                ut.UslugaComplex_id,
                at.AnalyzerTest_SysNick DESC,
                uc.UslugaComplex_Name DESC,
                "UslugaComplex_Name_Gost" DESC
        
