select ves.EvnSection_setDate, vdsp.DiagSetPhase_Code , vdsp.DiagSetPhase_Name,
		person_state.OnkoPersonStateEcog_id, person_state.OnkoPersonStateEcog_Name,
		person_state.OnkoPersonStateKarnovskiy_id, person_state.OnkoPersonStateKarnovskiy_Name,
		recist.MedicalFormDecision_Code ,recist.MedicalFormDecision_Name,
		lab.LabTest_Code ,lab.LabTest_Name,
		diag.FSIDI_Code ,diag.FSIDI_Name,drug.DrugComplexMnn_id,
		drug.ACTMATTERS_Code,drug.ACTMATTERS_NameRus, drug.EvnCourseTreatDrug_Kolvo
		,drug.EvnCourseTreat_Duration ,drug.DurationType_Name, move_review.EvnXml_Data
		from v_EvnSection ves   
           
     
		left join v_DiagSetPhase vdsp  
           
      ON vdsp.DiagSetPhase_id = ves.DiagSetPhase_id
		 
         left join lateral 
     
		(
		select
		vopse.OnkoPersonStateEcog_id, vopse.OnkoPersonStateEcog_Name,
		vopsk.OnkoPersonStateKarnovskiy_id, vopsk.OnkoPersonStateKarnovskiy_Name
		from dbo.v_MorbusOnko mo  
           
     
		left join dbo.v_MorbusOnkoBase mob  
           
      on mob.MorbusBase_id = mo.MorbusBase_id
		left join dbo.v_MorbusOnkoBasePersonState mobps  
           
      on mobps.MorbusOnkoBase_id = mob.MorbusOnkoBase_id
		left join v_OnkoPersonStateEcog vopse  
           
      ON vopse.OnkoPersonStateEcog_id = mobps.OnkoPersonStateEcog_id
		left join v_OnkoPersonStateKarnovskiy vopsk  
           
      ON vopsk.OnkoPersonStateKarnovskiy_id = mobps.OnkoPersonStateKarnovskiy_id
		where mo.Morbus_id = ves.Morbus_id
		and CAST(ves.EvnSection_setDT AS DATE) = CAST(mobps.MorbusOnkoBasePersonState_setDT AS DATE)
		) person_state
		 on true 
		 
         left join lateral 
     
		(
		select mfd.MedicalFormDecision_Code ,mfd.MedicalFormDecision_Name
		from dbo.v_EvnUsluga eu  
           
     
		inner join dbo.v_MedicalFormPerson mfp on mfp.Evn_id = ves.EvnSection_id
		left join dbo.MedicalFormDecisionPerson mfdp on mfdp.MedicalFormPerson_id = mfp.MedicalFormPerson_id
		left join dbo.MedicalFormDecision mfd on mfd.MedicalFormDecision_id = mfdp.MedicalFormDecision_id
		left join dbo.MedicalForm mf on mf.MedicalForm_id = mfd.MedicalForm_id
		where mf.MedicalFormType_id = 4 and eu.EvnUsluga_pid = ves.EvnSection_id
		) recist
		 on true 
		 
         left join lateral 
     
		(
		select ex.EvnXml_Data
		from dbo.v_EvnXml ex  
           
     
		where ex.Evn_id = ves.EvnSection_id
		and ex.XmlType_id = 8
		) move_review
		 on true 
		 
         left join lateral 
     
		(
		select lt.LabTest_Code ,lt.LabTest_Name
		from dbo.v_EvnUslugaPar eup  
           
     
		inner join dbo.v_EvnLabRequest elr  
           
      on elr.EvnDirection_id = eup.EvnDirection_id
		inner join dbo.v_EvnLabSample els  
           
      on els.EvnLabRequest_id = elr.EvnLabRequest_id
		inner join dbo.v_UslugaTest ut  
           
      on (ut.UslugaTest_pid = eup.EvnUslugaPar_id and ut.EvnLabSample_id = els.EvnLabSample_id)
		inner join nsi.LabTest lt  
           
      on  lt.LabTest_id = ut.LabTest_id
		where eup.EvnUslugaPar_pid = ves.EvnSection_id
		) lab
		 on true 
		 
         left join lateral 
     
		(
		select nsiFSIDI.FSIDI_Code ,nsiFSIDI.FSIDI_Name
		from dbo.v_EvnUslugaPar eup  
           
     
		inner join nsi.FSIDI nsiFSIDI  
           
      on nsiFSIDI.FSIDI_id = eup.FSIDI_id
		where eup.EvnUslugaPar_pid = ves.EvnSection_id
		) diag
		 on true 
		 
         left join lateral 
     
		(
		select acm.ACTMATTERS_Code
		,acm.ACTMATTERS_NameRus ,ectd.EvnCourseTreatDrug_Kolvo
		,ect.EvnCourseTreat_Duration ,dt.DurationType_Name, dcm.DrugComplexMnn_id
		from dbo.v_EvnCourseTreat ect  
           
     
		left join dbo.DurationType dt  
           
      on dt.DurationType_id = ect.DurationType_id
		left join dbo.EvnCourseTreatDrug ectd  
           
      on ectd.EvnCourseTreat_id = ect.EvnCourseTreat_id
		left join rls.DrugComplexMnn dcm  
           
      on dcm.DrugComplexMnn_id = ectd.DrugComplexMnn_id
		left join rls.DrugComplexMnnName dcmn on dcmn.DrugComplexMnnName_id = dcm.DrugComplexMnnName_id
		left join nsi.ACTMATTERSLink acml on acml.ACTMATTERS_rid = dcmn.ActMatters_id
		left join nsi.ACTMATTERS acm on acm.ACTMATTERS_id = acml.ACTMATTERS_nid
		where ect.EvnCourseTreat_pid = ves.EvnSection_id
		) drug
		 on true 
		where ves.EvnSection_rid = $1
