with rid as (select e.Evn_id as rid from v_EvnDirection ed  
    inner join v_Evn e   on e.Evn_id=ed.EvnDirection_rid
    where ed.EvnDirection_id=$1 )

		select vst.SMIType_CodeNsi, vst.SMIType_Nsi,smi2.UslugaComplex_Name as SMIType_FullName, smi2.EvnUslugaPar_id as common_evn_id
				, smi2.EvnUslugaPar_setDate as set_date
		 from vimis.SMIType vst  
		 left join lateral 
		(
			select eu.EvnUslugaPar_id, eu.EvnUslugaPar_setDate,uc.UslugaComplex_Name
			from dbo.v_EvnUslugaPar eu  
			inner join rid   on rid.rid=eu.EvnUslugaPar_rid
			left join v_UslugaComplex uc on uc.UslugaComplex_id=eu.UslugaComplex_id
			left join dbo.UslugaComplexAttribute uca    ON uca.UslugaComplex_id = eu.UslugaComplex_id
			left join dbo.UslugaComplexAttributeType ucat    ON ucat.UslugaComplexAttributeType_id = uca.UslugaComplexAttributeType_id
			where ucat.UslugaComplexAttributeType_SysNick = 'func'
		) smi2  on true 
		where vst.SMIType_Code = 'SMSV2'  and smi2.EvnUslugaPar_setDate is not null
		union
		select vst.SMIType_CodeNsi, vst.SMIType_Nsi,vst.SMIType_FullName , smi5.EvnUslugaTelemed_id as common_evn_id,
		  		smi5.EvnUslugaTelemed_setDate as set_date from vimis.SMIType vst  
		 left join lateral 
		(
			select eut.EvnUslugaTelemed_id,eut.EvnUslugaTelemed_setDate
		from dbo.v_EvnUslugaTelemed eut  
		inner join rid   on rid.rid=eut.EvnUslugaTelemed_rid
		) smi5  on true 
		 where vst.SMIType_Code = 'SMSV5' and smi5.EvnUslugaTelemed_setDate is not null
		  union 
		select vst.SMIType_CodeNsi, vst.SMIType_Nsi,smi3.UslugaComplex_Name as SMIType_FullName, smi3.EvnUslugaPar_id as common_evn_id
				, smi3.EvnUslugaPar_setDate as set_date
		 from vimis.SMIType vst   
		 left join lateral 
		(
			select eu.EvnUslugaPar_id, eu.EvnUslugaPar_setDate,uc.UslugaComplex_Name
			from dbo.v_EvnUslugaPar eu  
			inner join rid   on rid.rid=eu.EvnUslugaPar_rid
			left join v_UslugaComplex uc on uc.UslugaComplex_id=eu.UslugaComplex_id
			left join dbo.UslugaComplexAttribute uca    ON uca.UslugaComplex_id = eu.UslugaComplex_id
			left join dbo.UslugaComplexAttributeType ucat   ON ucat.UslugaComplexAttributeType_id = uca.UslugaComplexAttributeType_id
			where   ucat.UslugaComplexAttributeType_SysNick = 'lab'
		) smi3  on true 
		 where vst.SMIType_Code = 'SMSV3' and smi3.EvnUslugaPar_setDate is not null
