with prm as (
select getdate()::timestamp as reporttimestamp, --to_char('2020-09-04', 'YYYY-MM-DD')--to_char('2020-06-09', 'YYYY-MM-DD');
       getdate()::date::timestamp as reportDate,
       dateadd('minute', - 1, dateadd('day', 1, getdate()::date::timestamp)) as Date)
/*Pers as
(*/
  select *
	/*
         -- L.Lpu_id,
         -- Графа 14: Кол-во переведенных на лёгкие койки через выписку
         COUNT(distinct case
                          when (AttrHosp.cntCOVID_KoikiAll >= 0 or AttrHosp.cntPnevmo_KoikiAll >= 0 or AttrHosp.cntBerem_KoikiAll > 0) and ES.EvnSection_disDT between dateadd('hour', - 24, dateadd('minute', 1, (select Date from prm))) and (select Date from prm)
 and ES.LeaveType_id > 0
         --and LT.LeaveType_SysNick in ('ksother','ksper')
 and EPSNextRecover.EvnPS_id > 0
         --and ES.EvnSection_Count-1 = ES.EvnSection_index
         then EPS.Person_id
                        end) as cntPersCOVID_onRecoverFromHeavy_WithLeave
												*/
  from dbo.v_Lpu L
       inner join dbo.v_EvnPS EPS on EPS.Lpu_id = L.Lpu_id
           AND (L.Lpu_IsTest IS NULL OR L.Lpu_IsTest = 1) 
           and L.Lpu_begDate < dateadd('day', 1, (select ReportDate from prm))

           and (L.Lpu_endDate >= dateadd('day', - 7, (select ReportDate from prm)) or L.Lpu_endDate is NULL)
           and EPS.EvnPS_setDT between '01.01.2020'::DATE AND (select Date from prm)
           and (EPS.EvnPS_disDT > dateadd('hour', - 24 * 8, (select Date from prm)) or EPS.EvnPS_disDT is NULL)
