with prm as (
select GETDATE()::timestamp as Date,
       GETDATE()::timestamp as DT)--,
select *
 from 
 (select * from dbo.v_EvnPS EPS where EPS.EvnPS_setdate::date >= '01.01.2020' and EPS.EvnPS_setDate <= (select Date from prm)
 and (EPS.EvnPS_disDate > (select Date from prm) or EPS.EvnPS_disDate is null)) EPS
 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 Date from prm)) 
 and COALESCE(L.Lpu_endDate, (select Date from prm)) >= (select Date from prm)
 								  and L.lpu_ogrn in ('1035005503356','1035007200733', 
                                     '1035004250038', '1195027016480', '1025001201170', 
                                     '1025001624152', '1035005503356', '1025001466445', 
                                     '1035004465990', '1205000033358', '1025001278158', 
                                     '1035008256140', '1175022004816', '1025005599464', 
                                     '1025007389770', '1025004914967', '1025000845539', 
                                     '1025002590458', '1205000065764', '1065028003578', 
                                     '1035011654556', '1035003374262', '1035000701592', 
                                     '1215000105286', '1035006103538', '1195074004366', 
                                     '1025006174852', '1205000012381', '1195081091743', 
                                     '1025001767548', '1205000034799')
 limit 1) L on true        
 inner join lateral (select * from dbo.v_EvnSection ES where ES.EvnSection_pid = EPS.EvnPS_id 
 and ES.EvnSection_setDate <= (select Date from prm)
 and (ES.EvnSection_disDate > (select Date from prm)
 or ES.EvnSection_disDate is null) 
 and COALESCE(ES.EvnSection_IsPriem, 1) = 1 
 and ES.EvnSection_Count - 1 = ES.EvnSection_index) ES on true
 left join lateral (select * from v_Diag as Diag 
 where Diag.Diag_id = coalesce(ES.Diag_id, EPS.Diag_id, EPS.Diag_pid)
 and ((left (Diag.Diag_code, 3) between 'I60' and 'I65') or
        left (Diag.Diag_code, 3) in ('G45'))) Diag on true -- ES.Diag_id
 left join fed.v_LpuSectionBedProfileLink LSBPL on LSBPL.LpuSectionBedProfileLink_id = ES.LpuSectionBedProfileLink_fedid
 left join dbo.v_LpuSectionBedProfile LSBP on LSBP.LpuSectionBedProfile_id = LSBPL.LpuSectionBedProfile_id and
        LSBP.LpuSectionBedProfile_id in (3101, 3102, 3179, 3107)
 left join lateral (select * from dbo.v_EvnReanimatPeriod ERP where ERP.EvnReanimatPeriod_rid = EPS.EvnPS_id
 	and ERP.EvnReanimatPeriod_setDate <= (select Date from prm)
 	and (ERP.EvnReanimatPeriod_disDate > (select Date from prm)
 	or ERP.EvnReanimatPeriod_disDate is null)) ERP on true
       INNER JOIN LATERAL
       (
         select ESLast.EvnSection_id,
                ESLast.LpuSection_id,
                ESLast.Diag_id,
                COALESCE(ESLast.EvnSection_IsPriem, 1) as EvnSection_IsPriem,
                ESLast.EvnSection_setDT,
                ESLast.EvnSection_setDate,
                ESLast.EvnSection_disDate,
                ESLast.DiagSetPhase_id,
                ESLast.LpuSectionBedProfileLink_fedid
         from dbo.v_EvnSection ESLast
         where ESLast.EvnSection_pid = EPS.EvnPS_id and
               ESLast.EvnSection_setDate <= (select Date from prm)
         order by COALESCE(ESLast.EvnSection_IsPriem, 1),
                  ESLast.EvnSection_setDT desc
         limit 1
       ) ESLast on true
       inner join lateral (select * from dbo.v_LpuSection LS where LS.LpuSection_id = ES.LpuSection_id 
       and LS.LpuSectionProfile_id in (20000336, 20000297, 20000430) 
       and (ESLast.EvnSection_disDate > (select Date from prm)
       or ESLast.EvnSection_disDate is null)) LS on true
       inner join v_lpuunit LU on LU.lpuunit_id = LS.lpuunit_id and LU.lpuunittype_sysnick = 'stac'
       LEFT JOIN LATERAL
       (
         select RO.RepositoryObserv_id,
                RepositoryObserv_isIVL,
                RepositoryObserv_isOxygen,
                case
                  when RO.Evn_id = ERP.EvnReanimatPeriod_id then ERP.LpuSection_id
                  else ES.LpuSection_id
                end as LpuSection_id
         from dbo.v_RepositoryObserv RO
         where RO.Evn_id in (ES.EvnSection_id, ERP.EvnReanimatPeriod_id) and
               RO.RepositoryObserv_setDT < dateadd('day', 1, (select Date from prm))
         order by RO.RepositoryObserv_setDT desc
         limit 1
       ) RO on true
       LEFT JOIN LATERAL
       (
         select era.EvnReanimatAction_id
         from v_EvnReanimatAction ERA
         where ERA.EvnReanimatAction_pid = ERP.EvnReanimatPeriod_id and
               era.EvnReanimatAction_setDt < dateadd('day', 1, (select Date from prm)) and
               era.ReanimatActionType_id = 1
         order by era.EvnReanimatAction_setDT desc
         limit 1
       ) era on true
