with prm as
(
  select
     to_timestamp('01.02.2022', 'DD.MM.YYYY')::timestamp as p_BegDate,
     to_timestamp('05.02.2022', 'DD.MM.YYYY')::timestamp as p_EndDate
)

select
  L.Lpu_OGRN as "ОГРН ЮрЛица",
  EPS.EvnPS_setDate
from prm
inner join dbo.v_EvnPS EPS on EPS.EvnPS_setDate between p_BegDate and p_EndDate
inner join lateral
(
  select *
  from dbo.v_Lpu L
  where
    EPS.Lpu_id = L.Lpu_id
    and COALESCE(L.Lpu_IsTest, 1) = 1
    --and L.Lpu_begDate < dateadd('day', 1, (select EndDate from params))
    --and COALESCE(L.Lpu_endDate, (select BegDate from params)) >= (select BegDate from params)
   limit 1
) L on true
