
            with curD as (
                select cast(localtimestamp as date) as curDate
            ), mv as (
                select
                    70101000077002 as msf_id
            ), EU as (
            select
            Diag_id,
            EvnUsluga_updDT as DT
            from v_EvnUsluga
            where MedStaffFact_id = (select msf_id from mv)
            and Diag_id IS NOT NULL
            order by DT desc
            limit 20),

            EPL as (
            select
            Diag_id,
            EvnPL_updDT as DT
            from v_EvnPl
            where MedPersonal_id = (
                SELECT MedPersonal_id
                FROM v_MedStaffFact
                where MedStaffFact_id = (select msf_id from mv)
            )
            and Diag_id IS NOT NULL
            order by DT desc
            limit 20),

            EVPL as (
            select
            Diag_id,
            EvnVizitPL_updDT as DT
            from v_EvnVizitPL
            where MedStaffFact_id = (select msf_id from mv)
            and Diag_id IS NOT NULL
            order by DT desc
            limit 20),

            EPS as (
            select
            Diag_id,
            EvnPS_updDT as DT
            from v_EvnPS
            where MedStaffFact_id = (select msf_id from mv)
            and Diag_id IS NOT NULL
            order by DT desc
            limit 20),

            ES as (
            select
            Diag_id,
            EvnSection_updDT as DT
            from v_EvnSection
            where MedStaffFact_id = (select msf_id from mv)
            and Diag_id IS NOT NULL
            order by DT desc
            limit 20),


            Diags as (
                SELECT * from EU

                UNION ALL

                SELECT * from EPL

                UNION ALL

                SELECT * from EVPL

                UNION ALL

                SELECT * from EPS

                UNION ALL

                SELECT * from ES
                )

            Select
            D.Diag_id as "Diag_id",
            D.Diag_pid as "Diag_pid",
            D.Diag_Code as "Diag_Code",
            D.Diag_Name as "Diag_Name",
            D.DiagLevel_id as "DiagLevel_id",
            to_char(D.Diag_endDate, 'dd.mm.yyyy') as "Diag_endDate",
            DL.DiagLevel_Code as "DiagLevel_Code",
            DL.DiagLevel_Name as "DiagLevel_Name",
            DT as "DT",
            IsOms.YesNo_Code as "DiagFinance_IsOms",
            D.Diag_isPairedOrgans as "Diag_isPairedOrgans",
            vd.VimisDiag_id as "VimisDiag_id",
            vd.VimisType_id as "VimisType_id",
            df.DiagFinance_IsFacult as "DiagFinance_IsFacult"
            from Diags
            inner join v_Diag D on D.Diag_id = Diags.Diag_id
            inner join DiagLevel DL on DL.DiagLevel_id = D.DiagLevel_id
            left join v_DiagFinance df on df.Diag_id = D.Diag_id
            left join YesNo IsOms on IsOms.YesNo_id = df.DiagFinance_IsOms
            -- left join YesNo IsFacult on IsFacult.YesNo_id = df.DiagFinance_IsFacult
            left join vimis.v_VimisDiag vd on
                vd.Diag_id = D.Diag_id
                and (vd.VimisDiag_endDate is null or vd.VimisDiag_endDate > (select curDate from curD))

            order by DT desc
        
