select 
    --Lpu_s.Lpu_Name lpu_s_name,
	Lpu.lpu_name "Юридическое лицо",
	Lpu.Lpu_Ogrn "ОГРН",
es.* 
from ((
  select 
    L.Lpu_id,
	L.Lpu_Name,
	L.Lpu_Ogrn
  from dbo.v_Lpu L 
  where 
    coalesce(L.Lpu_IsTest, 1) = 1 )) as Lpu  
    	inner join v_EvnPS as EPS  on Lpu.Lpu_id =EPS.lpu_id
    and EPS.EvnPS_setDate::date between (now()-interval '30 days' )::date and now()::date--30
    inner join v_EvnSection as ES  on ES.EvnSection_pid = EPS.EvnPS_id
		and ES.EvnSection_Count-1 = ES.EvnSection_index	
		and EPS.PrehospWaifRefuseCause_id is null
	inner join v_lpusection as lpusection  on lpusection.lpusection_id =ES.lpusection_id
	inner join v_LpuUnit LU  on LU.LpuUnit_id = lpusection.LpuUnit_id
		and LU.LpuUnitType_SysNick in ('stac')
	inner join v_PersonState as PS  on EPS.Person_id = PS.Person_id
	inner join v_Diag as D  on D.Diag_id = coalesce(EPS.Diag_pid,ES.Diag_id)
