
                select
                    eup.EvnUslugaPar_id as "EvnUslugaPar_id",
                    'unlink' as "type"
                from
                    v_EvnUslugaPar eup
                where
                    eup.EvnUslugaPar_pid = '10101166277823'
                    and coalesce(eup.EvnUslugaPar_IsManual, 1) = 1
                    and (cast(eup.EvnUslugaPar_setDT as timestamp) < cast('2023-06-30 09:23' as timestamp) OR (cast(eup.EvnUslugaPar_setDT as timestamp) > cast('2023-07-07 10:00' as timestamp) and '2023-07-07 10:00' is not null))

                union all

                select
                    eup.EvnUslugaPar_id as "EvnUslugaPar_id",
                    'link' as "type"
                from
                    v_EvnUslugaPar eup
                    inner join v_EvnPrescrDirection epd on epd.EvnDirection_id = eup.EvnDirection_id
                    inner join v_EvnPrescr ep on ep.EvnPrescr_id = epd.EvnPrescr_id -- EvnPrescr
                    inner join v_EvnSection es on 	es.EvnSection_id = ep.EvnPrescr_pid
                    inner join v_EvnPS eps on eps.EvnPS_id = es.EvnSection_pid
                where
                    eup.EvnUslugaPar_pid is null
                    and coalesce(eup.EvnUslugaPar_IsManual, 1) = 1
                    and eps.EvnPS_id = '10101166277819'
                    and (cast(eup.EvnUslugaPar_setDT as timestamp) >= cast('2023-06-30 09:23' as timestamp) AND (cast(eup.EvnUslugaPar_setDT as timestamp) <= cast('2023-07-07 10:00' as timestamp) OR '2023-07-07 10:00' is null))

                union all

                select
                    eup.EvnUslugaPar_id as "EvnUslugaPar_id",
                    'link' as "type"
                from
                    v_EvnUslugaPar eup
                    inner join v_EvnPrescrDirection epd on epd.EvnDirection_id = eup.EvnDirection_id
                    inner join v_EvnPrescr ep on ep.EvnPrescr_id = epd.EvnPrescr_id -- EvnPrescr
                    inner join v_EvnPS eps on eps.EvnPS_id = ep.EvnPrescr_pid
                where
                    eup.EvnUslugaPar_pid is null
                    and coalesce(eup.EvnUslugaPar_IsManual, 1) = 1
                    and eps.EvnPS_id = '10101166277819'
                    and (cast(eup.EvnUslugaPar_setDT as timestamp) >= cast('2023-06-30 09:23' as timestamp) AND (cast(eup.EvnUslugaPar_setDT as timestamp) <= cast('2023-07-07 10:00' as timestamp) OR '2023-07-07 10:00' is null))
            
