select
	EvnPS.EvnPS_id,
	EvnSection.EvnSection_id,
	EvnSection.mes_sid,
	EvnSection.mes_tid,
	EvnSection.evnsection_iswillpaid,
	EvnSection.mestariff_id, 
	EvnSection.mesolduslugacomplex_id,
	EvnSection.diag_id,
	array(select drugtherapyscheme_id from evnsectiondrugtherapyscheme where evnsectiondrugtherapyscheme.evnsection_id = EvnSection.evnsection_id) AS drug_sheme_arr,
	array(select mesdop_id from mesdoplink where mesdoplink.evnsection_id = EvnSection.evnsection_id) AS mes_dop_arr
from v_EvnPS EvnPS
 join v_EvnSection as EvnSection on EvnSection.EvnSection_pid = EvnPS.EvnPS_id and EvnPS.Lpu_id = EvnSection.Lpu_id
	join dbo.v_LpuSection AS LpuSection on LpuSection.lpusection_id = EvnSection.lpusection_id
	JOIN dbo.v_LpuUnit AS LpuUnit ON LpuSection.lpuunit_id = LpuUnit.lpuunit_id 
 join v_EvnLeaveBase as EvnLeaveBase on EvnLeaveBase.EvnLeaveBase_pid = EvnSection.EvnSection_id and EvnLeaveBase_index = EvnLeaveBase_count - 1
 left join LeaveType on EvnPS.leavetype_id = LeaveType.leavetype_id
where
     EvnPS.EvnPS_disDate between '2022-01-01' and '2022-01-31'
     and EvnSection.paytype_id = 520101000000009
     and EvnSection.LeaveType_id is not null
     AND NULLIF(EvnSection.EvnSection_IsPriem, 1) IS NULL
