select 
			case $1 when 'SMSV1' then 0 else 1 end as diagTypeCode
			, case $2 when 'SMSV1' then 1 when 'SMSV4' then 4 when 'SMSV5' then case when spec.ttype_id is null then 1 else 3 end when 'SMSV6' then 3 when 'SMSV7' then 3  
			when 'SMSV8' then 3 when 'SMSV9' then 4  when 'SMSV10' then 3 when 'SMSV12' then 3 when 'SMSV11' then 3 when 'SMSV16' then 3 end as diagDegreeCode
			, case $3 when 'SMSV1' then 'Предварительный диагноз' when 'SMSV4' then 'Патологоанатомический диагноз' 
			when 'SMSV5' then case when spec.ttype_id is null then 'Предварительный диагноз' else 'Заключительный клинический диагноз' end 
			when 'SMSV6' then 'Заключительный клинический диагноз' 
			when 'SMSV7' then 'Заключительный клинический диагноз' when 'SMSV8' then 'Заключительный клинический диагноз'  when 'SMSV9' then 'Патологоанатомический диагноз' 
			when 'SMSV10' then 'Заключительный клинический диагноз'  when 'SMSV12' then 'Заключительный клинический диагноз' when 'SMSV11' then 'Заключительный клинический диагноз'  when 'SMSV16' then 'Заключительный клинический диагноз' end as diagDegreeName
			, mrbpt.PassportToken_tid as mrbMoId, mrbd.Diag_SCode as mrbDiag_SCode, mrbd.Diag_Name as mrbDiag_Name, 1 as level_code, 'Основное заболевание' as level_name
			, mrb.Morbus_id, mrbo.MorbusOnko_id, case when MorbusKind_Code = 1 then 3 when MorbusKind_Code = 2 then 1 else 3 end as mrb_kind_code, case when MorbusKind_Code = 1 then 'Острое' when MorbusKind_Code = 2 then 'Впервые в жизни установленное хроническое' else 'Острое' end as mrb_kind_name
			, ttype.TumorPrimaryMultipleType_Code, ttype.TumorPrimaryMultipleType_Name, coalesce(ols.OnkoLesionSide_Code,'4') as OnkoLesionSide_Code, coalesce(ols.OnkoLesionSide_Name,'неприменимо') as OnkoLesionSide_Name, case when od.OnkoDiag_Code is null then null else concat('M-', od.OnkoDiag_Code) end as OnkoDiag_Code, od.OnkoDiag_Name
			, odct.OnkoDiagConfType_Code, odct.OnkoDiagConfType_Name, mrbo.MorbusOnko_NumTumor as NumTumor, cast(case when mrbo.MorbusOnko_IsMainTumor is null then null when mrbo.MorbusOnko_IsMainTumor = 2 then 1 else 0 end as bit) as IsMainTumor
			, yn.YesNo_Name as IsMainTumorYN, tcit.TumorCircumIdentType_Code, tcit.TumorCircumIdentType_Name, coalesce(mrbo.MorbusOnko_setDiagDT, entity.diagDT) as Morbus_setDT

			, catT.TNMCategory_Code as OnkoT_Code, concat(catT.TNMCategory_SysNick, ' - ' , catT.TNMCategory_Name) as OnkoT_Name
			, catN.TNMCategory_Code as OnkoN_Code, concat(catN.TNMCategory_SysNick, ' - ' , catN.TNMCategory_Name) as OnkoN_Name
			, catM.TNMCategory_Code as OnkoM_Code, concat(catM.TNMCategory_SysNick, ' - ' , catM.TNMCategory_Name) as OnkoM_Name
			, catG.TNMCategory_Code as OnkoG_Code, concat(catG.TNMCategory_SysNick, ' - ' , catG.TNMCategory_Name) as OnkoG_Name
			, stad.TNMStadia_Code as OnkoTNMDiag_Code, stage.TNMStage_Name as OnkoTNMDiag_Name

			, tstage.TumorStage_Code, tstage.TumorStage_Name
			, clgr.OnkoClinicGroup_Code, clgr.OnkoClinicGroup_Name, dbo.Age(pr.Person_BirthDay, coalesce(mrbo.MorbusOnko_setDiagDT, entity.diagDT)) as age, vpc.VimisPatientControl_begDate
			, 2 as firstTimeCredCode, 'На основании медицинской документации' as firstTimeCredName
			, entity.IsSuspect, entity.Evn_setDT as SuspectDate, suspdiag.Diag_SCode as sspDiag_SCode, suspdiag.Diag_Name as sspDiag_Name
			, case when spec.IsTumorDepoBones = 2 then 2 
			when spec.IsTumorDepoBrain = 2 then 5
			when spec.IsTumorDepoKidney = 2 then 7
			when spec.IsTumorDepoLiver = 2 then 3
			when spec.IsTumorDepoLungs = 2 then 4
			when spec.IsTumorDepoLympha = 2 then 1
			when spec.IsTumorDepoMarrow = 2 then 10
			when spec.IsTumorDepoMulti = 2 then 12
			when spec.IsTumorDepoOther = 2 then 11
			when spec.IsTumorDepoOvary = 2 then 8
			when spec.IsTumorDepoPerito = 2 then 9
			when spec.IsTumorDepoSkin = 2 then 6
			when spec.IsTumorDepoUnknown = 2 then 13
			else null end as localis_code
			, case when spec.IsTumorDepoBones = 2 then 'кости'
			when spec.IsTumorDepoBrain = 2 then 'головной мозг'
			when spec.IsTumorDepoKidney = 2 then 'почки'
			when spec.IsTumorDepoLiver = 2 then 'печень'
			when spec.IsTumorDepoLungs = 2 then 'легкие и/или плевра'
			when spec.IsTumorDepoLympha = 2 then 'отдаленные лимфатические узлы'
			when spec.IsTumorDepoMarrow = 2 then 'костный мозг'
			when spec.IsTumorDepoMulti = 2 then '12'
			when spec.IsTumorDepoOther = 2 then 'другие органы'
			when spec.IsTumorDepoOvary = 2 then 'яичники'
			when spec.IsTumorDepoPerito = 2 then 'брюшина'
			when spec.IsTumorDepoSkin = 2 then 'кожа'
			when spec.IsTumorDepoUnknown = 2 then 'неизвестна'
			else null end as localis_name
			
			from 
			 
		(select coalesce(evd.Diag_id, e.Diag_id) as diag_id, mrb.Morbus_id, case when e.EvnVizitPL_IsZNO = 2 and coalesce(e.EvnVizitPL_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, e.Diag_spid, evn.Evn_setDT 
		, epar.Evn_setDT as diagDT
		from v_EvnVizitPL e  
		inner join v_EvnVizit vz   on e.EvnVizitPL_id = vz.EvnVizit_id
		left join dbo.v_Evn evn   on evn.Evn_id = $4
		left join dbo.Evn epar   on epar.Evn_id = evn.Evn_pid
		left join EvnDiag evd   on evd.Evn_id = vz.EvnVizit_id
		left join dbo.v_Morbus mrb   on $5 = mrb.Person_id and e.Diag_id = mrb.Diag_id
		where e.EvnVizitPL_id = $6 and ($7 = 'SMSV5')
		union 
		select e.Diag_id, mrb.Morbus_id, null as IsSuspect, null as Diag_spid, evn.Evn_setDT, epar.Evn_setDT as diagDT
		from v_EvnVizitDisp e  
		left join dbo.v_Evn evn   on evn.Evn_id = $8
		left join dbo.Evn epar   on epar.Evn_id = evn.Evn_pid
		left join dbo.v_Morbus mrb   on $9 = mrb.Person_id and e.Diag_id = mrb.Diag_id
		where 
		e.EvnVizitDisp_id = $10 and ($11 = 'SMSV5')
		union 
		select e.Diag_id,mrb.Morbus_id, null as IsSuspect, null as Diag_spid, evn.Evn_setDT, epar.Evn_setDT as diagDT
		from v_EvnUslugaTelemed e  
		left join dbo.v_Evn evn   on evn.Evn_id = $12
		left join dbo.Evn epar   on epar.Evn_id = evn.Evn_pid
		left join dbo.v_Morbus mrb   on $13 = mrb.Person_id and e.Diag_id = mrb.Diag_id
		where 
		e.EvnUslugaTelemed_id = $14 and ($15 = 'SMSV5')
		union 
		select coalesce(e.Diag_id, pl.Diag_id, es.Diag_id) Diag_id, mrb.Morbus_id, case when pl.EvnVizitPL_id is NULL then NULL when pl.EvnVizitPL_IsZNO = 2 and coalesce(pl.EvnVizitPL_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, pl.Diag_spid, pl.EvnVizitPL_setDT as Evn_setDT
		, coalesce(eviz.Evn_setDT, eps.EvnDirection_setDT, epar.Evn_setDT) as diagDT
		from v_EvnDirection_all e  
		left join dbo.v_EvnVizitPL pl   on pl.EvnVizitPL_id = e.EvnDirection_pid
		left join dbo.Evn eviz   on eviz.Evn_id = pl.EvnVizitPL_id
		left join dbo.v_EvnSection es   on es.EvnSection_id = e.EvnDirection_pid
		left join dbo.v_EvnPS eps   on es.EvnSection_rid = eps.EvnPS_id
		left join dbo.Evn epar   on epar.Evn_id = e.EvnDirection_pid
		left join dbo.v_Morbus mrb   on $16 = mrb.Person_id and coalesce(e.Diag_id, pl.Diag_id, es.Diag_id) = mrb.Diag_id
		where 
		e.EvnDirection_id = $17 and ($18 = 'SMSV1')
		union 
		select e.Diag_id,mrb.Morbus_id, case when coalesce(pl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) is NULL then NULL when coalesce(pl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) = 2 and coalesce(pl.EvnVizitPL_IsZNORemove, ps.EvnPS_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, coalesce(pl.Diag_spid, ps.Diag_spid) as Diag_spid, epid.Evn_setDT 
		, coalesce(epar.Evn_setDT, ps.EvnDirection_setDT, edh.EvnDirectionHistologic_setDate) as diagDT
		from v_EvnHistologicProto e  
		left join dbo.v_Evn evn   on evn.Evn_id = $19
		left join dbo.v_Morbus mrb   on $20 = mrb.Person_id and e.Diag_id = mrb.Diag_id
		left join dbo.v_EvnDirectionHistologic edh   on edh.EvnDirectionHistologic_id = e.EvnDirectionHistologic_id
        left join dbo.Evn epid   on epid.Evn_id = edh.EvnDirectionHistologic_pid 
		left join dbo.v_EvnVizitPL pl   on pl.EvnVizitPL_id = epid.Evn_id and epid.EvnClass_id=11
		left join dbo.Evn epar   on epar.Evn_id = pl.EvnVizitPL_pid
		left join dbo.v_EvnSection es   on  es.EvnSection_id = epid.Evn_id and epid.EvnClass_id=32		
		left join dbo.v_EvnPS ps   on ps.EvnPS_id = es.EvnSection_rid
		where 
		e.EvnHistologicProto_id = $21 and ($22 = 'SMSV4')
		union 
		select   e.Diag_id, coalesce(pr.Morbus_id, m.Morbus_id) as Morbus_id, case when pl.EvnVizitPL_IsZNO is NULL then NULL when pl.EvnVizitPL_IsZNO = 2 and coalesce(pl.EvnVizitPL_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, pl.Diag_spid, pl.EvnVizitPL_setDT as Evn_setDT  
		, e.PersonDisp_begDate as diagDT
		from v_PersonDisp e  
		left join dbo.PersonRegisterDispLink pdl   on e.PersonDisp_id = pdl.PersonDisp_id
		left join dbo.PersonRegister pr   on pr.PersonRegister_id = pdl.PersonRegister_id
		 left join lateral  (select   pl.EvnVizitPL_id, pl.EvnVizitPL_IsZNO, pl.EvnVizitPL_IsZNORemove, pl.Diag_spid, pl.EvnVizitPL_setDT
		from v_EvnVizitPL pl   
		where pl.PersonDisp_id = e.PersonDisp_id order by pl.EvnVizitPL_setDT desc  limit 1 ) pl  on true 
		left join dbo.v_EvnVizit vz   on vz.EvnVizit_id = pl.EvnVizitPL_id
		left join dbo.Morbus m   on m.Evn_pid = vz.EvnVizit_id
		where 
		e.PersonDisp_id = $23 and (($24 = 'SMSV7') or ($25 = 'SMSV16'))
		union
		select e.Diag_id,e.Morbus_id, case when ps.EvnPS_IsInRegZNO = 2 and coalesce(ps.EvnPS_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, ps.Diag_spid, evn.Evn_setDT 
		, ps.EvnDirection_setDT as diagDT
		from dbo.v_EvnSection e  
		left join dbo.v_EvnPS ps   on ps.EvnPS_id = e.EvnSection_rid
		left join dbo.v_Evn evn   on evn.Evn_id = $26
		where 
		e.EvnSection_id = $27 and ($28 = 'SMSV8')
		union 
		select coalesce(diag.Diag_id, edc_diag.Diag_id) as diag_id, coalesce(ecp.Morbus_id, pl.Morbus_id) as Morbus_id, case when coalesce(pl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) is NULL then NULL when coalesce(pl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) = 2 and coalesce(pl.EvnVizitPL_IsZNORemove, ps.EvnPS_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, coalesce(pl.Diag_spid, ps.Diag_spid) as Diag_spid, epid.Evn_setDT
		, coalesce(epar.Evn_setDT, ps.EvnDirection_setDT) as diagDT
		from v_EvnCytologicProto ecp  
		left join dbo.v_Evn evn   on evn.Evn_id = $29
		left join fed.Mkb10Code fmkb   on fmkb.Mkb10Code_id = ecp.Mkb10Code_id
		left join dbo.v_Diag diag   on diag.Diag_SCode = fmkb.Mkb10Code_StateCode
		left join dbo.v_EvnDirectionCytologic edc   on edc.EvnDirectionCytologic_id = ecp.EvnDirectionCytologic_id
		left join dbo.v_EvnDirectionCytologic edc0   on edc0.EvnDirectionCytologic_id = ecp.EvnDirectionCytologic_id
		left join dbo.v_Diag edc_diag   on edc_diag.Diag_id = edc.Diag_id
		left join dbo.v_EvnDirection dir   on dir.EvnDirection_id = edc0.EvnDirectionCytologic_id
		left join dbo.Evn ed   on ed.Evn_id = dir.EvnDirection_id
        left join dbo.Evn epid   on epid.Evn_id = ed.Evn_pid 
		left join dbo.v_EvnVizitPL pl   on pl.EvnVizitPL_id = epid.Evn_id and epid.EvnClass_id=11
		left join dbo.Evn epar   on epar.Evn_id = pl.EvnVizitPL_pid
		left join dbo.v_EvnSection es   on  es.EvnSection_id = epid.Evn_id and epid.EvnClass_id=32		
		left join dbo.v_EvnPS ps   on ps.EvnPS_id = es.EvnSection_rid
		where 
		ecp.EvnCytologicProto_id = $30 and ($31 = 'SMSV9')
		union 
		select e.Diag_id, e.Morbus_id, case when coalesce(pl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) is NULL then NULL when coalesce(pl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) = 2 and coalesce(pl.EvnVizitPL_IsZNORemove, ps.EvnPS_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, coalesce(pl.Diag_spid, ps.Diag_spid) as Diag_spid, erid.Evn_setDT 
		, coalesce(pl.diagDT, ps.EvnDirection_setDT) as diagDT
		from  v_EvnUslugaOnkoSurg e  
		left join dbo.v_Evn evn   on evn.Evn_id = $32
		left join dbo.v_Evn erid   on erid.Evn_id = e.EvnUslugaOnkoSurg_rid 
		 left join lateral  (select   pl.EvnVizitPL_IsZNO, pl.EvnVizitPL_IsZNORemove, pl.Diag_spid, epar.Evn_setDT as diagDT 
		from v_EvnPL epl   
		left join v_EvnVizitPL pl   on pl.EvnVizitPL_rid = epl.EvnPL_id
		left join dbo.Evn epar   on epar.Evn_id = pl.EvnVizitPL_pid
		where erid.Evn_id = epl.EvnPL_id and erid.EvnClass_SysNick = 'EvnPL' order by pl.EvnVizitPL_setDT desc  limit 1 ) pl  on true 
		left join dbo.v_EvnPS ps   on ps.EvnPS_id = erid.Evn_id and erid.EvnClass_SysNick = 'EvnPS'
		where 
		e.EvnUslugaOnkoSurg_id = $33 and ($34 = 'SMSV10')
		union
		select null as Diag_id, e.Morbus_id, case when coalesce(pl.EvnVizitPL_IsZNO, epl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) is NULL then NULL when coalesce(pl.EvnVizitPL_IsZNO, epl.EvnVizitPL_IsZNO, ps.EvnPS_IsInRegZNO) = 2 and coalesce(pl.EvnVizitPL_IsZNORemove, epl.EvnVizitPL_IsZNORemove, ps.EvnPS_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, coalesce(pl.Diag_spid, epl.Diag_spid, ps.Diag_spid) as Diag_spid, coalesce(pl.EvnVizitPL_setDT, epl.EvnVizitPL_setDT, ps.Evn_setDT) as Evn_setDT
		, coalesce(pl.diagDT, epl.diagDT, ps.diagDT) as diagDT
		from  v_EvnOnkoNotifyNeglected e  
		left join v_EvnOnkoNotify eon   on eon.EvnOnkoNotify_id = e.EvnOnkoNotify_id
		 left join lateral  (select   pl.EvnVizitPL_IsZNO, pl.EvnVizitPL_IsZNORemove, pl.Diag_spid, pl.EvnVizitPL_setDT, epar.Evn_setDT as diagDT  
		from v_Evn epid inner join v_EvnVizitPL pl   on pl.EvnVizitPL_id = epid.Evn_id 
		left join dbo.Evn epar   on epar.Evn_id = pl.EvnVizitPL_pid
		where epid.Evn_id = eon.EvnOnkoNotify_pid and epid.EvnClass_SysNick = 'EvnVizitPL'
		order by pl.EvnVizitPL_setDT desc  limit 1 ) pl  on true 
		 left join lateral  (select   pl.EvnVizitPL_IsZNO, pl.EvnVizitPL_IsZNORemove, pl.Diag_spid, pl.EvnVizitPL_setDT, epar.Evn_setDT as diagDT  
		from v_Evn erid inner join v_EvnPL epl   on epl.EvnPL_id = erid.Evn_id 
		left join v_EvnVizitPL pl   on pl.EvnVizitPL_rid = epl.EvnPL_id
		left join dbo.Evn epar   on epar.Evn_id = pl.EvnVizitPL_pid
		where  erid.Evn_id = eon.EvnOnkoNotify_rid and erid.EvnClass_SysNick = 'EvnPL' 
		order by pl.EvnVizitPL_setDT desc  limit 1 ) epl  on true 
		 left join lateral  (select   eps.EvnPS_IsInRegZNO, eps.EvnPS_IsZNORemove, eps.Diag_spid, erid.Evn_setDT, eps.EvnDirection_setDT as diagDT
		from v_Evn erid inner join v_EvnPS eps   on eps.EvnPS_id = erid.Evn_id
		where  erid.Evn_id = eon.EvnOnkoNotify_rid and erid.EvnClass_SysNick = 'EvnPS'  limit 1 ) ps  on true 
		where 
		e.EvnOnkoNotifyNeglected_id = $35 and ($36 = 'SMSV11')
		union 
		select   e.Diag_id, mrb.Morbus_id, case when pl.EvnVizitPL_IsZNO is NULL then NULL when pl.EvnVizitPL_IsZNO = 2 and coalesce(pl.EvnVizitPL_IsZNORemove, 1) <> 2 then 2 else 1 end as IsSuspect, pl.Diag_spid, evn.Evn_setDT
		, pl.diagDT
		from v_EvnPL e  
		left join dbo.v_Evn evn   on evn.Evn_id = $37
		left join EvnDiag evd    on evd.Evn_id = e.EvnPl_id
		left join dbo.v_Morbus mrb    on $38 = mrb.Person_id and e.Diag_id = mrb.Diag_id
		 left join lateral  (select   pl.EvnVizitPL_IsZNO, pl.EvnVizitPL_IsZNORemove, pl.Diag_spid, epar.Evn_setDT as diagDT 
		from v_EvnVizitPL pl   
		left join dbo.Evn epar   on epar.Evn_id = pl.EvnVizitPL_pid
		where pl.EvnVizitPL_rid = e.EvnPL_id order by pl.EvnVizitPL_setDT desc  limit 1 ) pl  on true 
		where e.EvnPL_id = $39 and ($40 = 'SMSV12')
		 limit 1 
		) entity
	 
			
			left join v_Morbus mrb    on mrb.Morbus_id = entity.Morbus_id
			left join v_PersonState pr   on mrb.Person_id = pr.Person_id
			left join v_MorbusOnko mrbo   on mrb.Morbus_id = mrbo.Morbus_id
			left join v_Lpu mrblpu   on mrbo.Lpu_foid = mrblpu.Lpu_id
            LEFT JOIN fed.v_PassportToken mrbpt   ON mrblpu.Lpu_id = mrbpt.Lpu_id
			left join v_Diag mrbd   on  mrbd.Diag_id = coalesce(mrb.Diag_id, entity.Diag_id)
			left join v_MorbusKind mrbk   on mrbk.MorbusKind_id = mrb.MorbusKind_id


	         left join lateral 
			(select   coalesce(mrbPL.TumorPrimaryMultipleType_id, mrbS.TumorPrimaryMultipleType_id) as ttype_id
			, coalesce(mrbPL.OnkoT_id, mrbS.OnkoT_id) as OnkoT_id, coalesce(mrbPL.OnkoN_id, mrbS.OnkoN_id) as OnkoN_id, coalesce(mrbPL.OnkoM_id, mrbS.OnkoM_id) as OnkoM_id
			, coalesce(mrbPL.diag_id, mrbS.diag_id) as diag_id, coalesce(mrbPL.TumorStage_id, mrbS.TumorStage_id) as TumorStage_id
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoBones, mrbS.MorbusOnkoLeave_IsTumorDepoBones) as IsTumorDepoBones
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoBrain, mrbS.MorbusOnkoLeave_IsTumorDepoBrain) as IsTumorDepoBrain
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoKidney, mrbS.MorbusOnkoLeave_IsTumorDepoKidney) as IsTumorDepoKidney
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoLiver, mrbS.MorbusOnkoLeave_IsTumorDepoLiver) as IsTumorDepoLiver
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoLungs, mrbS.MorbusOnkoLeave_IsTumorDepoLungs) as IsTumorDepoLungs
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoLympha, mrbS.MorbusOnkoLeave_IsTumorDepoLympha) as IsTumorDepoLympha
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoMarrow, mrbS.MorbusOnkoLeave_IsTumorDepoMarrow) as IsTumorDepoMarrow
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoMulti, mrbS.MorbusOnkoLeave_IsTumorDepoMulti) as IsTumorDepoMulti
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoOther, mrbS.MorbusOnkoLeave_IsTumorDepoOther) as IsTumorDepoOther
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoOvary, mrbS.MorbusOnkoLeave_IsTumorDepoOvary) as IsTumorDepoOvary
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoPerito, mrbS.MorbusOnkoLeave_IsTumorDepoPerito) as IsTumorDepoPerito
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoSkin, mrbS.MorbusOnkoLeave_IsTumorDepoSkin) as IsTumorDepoSkin
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_IsTumorDepoUnknown, mrbS.MorbusOnkoLeave_IsTumorDepoUnknown) as IsTumorDepoUnknown

			, coalesce(plstad.TNMStadia_id, sstad.TNMStadia_id) as TNMStadia_id, mrbo.Morbus_id
			, coalesce(mrbPL.MorbusOnkoVizitPLDop_insDT, mrbS.MorbusOnkoLeave_insDT ) as insDT

			from dbo.v_Evn evn   
			left join v_MorbusOnkoVizitPLDop mrbPL    on mrbPL.EvnVizit_id = evn.Evn_id and mrb.Diag_id = mrbPL.Diag_id and evn.EvnClass_SysNick = 'EvnVizitPL'
			left join v_MorbusOnkoLeave mrbS   on mrbS.EvnSection_id = evn.Evn_id and mrb.Diag_id = mrbS.Diag_id and evn.EvnClass_SysNick = 'EvnSection'

			left join v_MorbusOnkoTNMStadia plstad   on plstad.MorbusOnkoVizitPLDop_id = mrbPL.MorbusOnkoVizitPLDop_id
			left join v_MorbusOnkoTNMStadia sstad   on sstad.MorbusOnkoLeave_id = mrbS.MorbusOnkoLeave_id
			left join v_MorbusOnkoLink plmol   on plmol.MorbusOnkoVizitPLDop_id = mrbPL.MorbusOnkoVizitPLDop_id 
			left join v_MorbusOnkoLink smol   on smol.MorbusOnkoLeave_id = mrbS.MorbusOnkoLeave_id
			left join v_MorbusOnko mrbo   on mrbo.MorbusOnko_id = coalesce(plmol.MorbusOnko_id, smol.MorbusOnko_id)
			
			where mrb.Person_id = evn.Person_id and coalesce(mrbPL.MorbusOnkoVizitPLDop_id, mrbS.MorbusOnkoLeave_id) is not null 
			order by evn.Evn_setDT desc, coalesce(plstad.MorbusOnkoTNMStadia_insDT, sstad.MorbusOnkoTNMStadia_insDT) desc
	         limit 1 
			) spec  on true 
			
			left join v_TumorPrimaryMultipleType ttype   on spec.ttype_id = ttype.TumorPrimaryMultipleType_id


			left join nsi.TNMStadia stad   on spec.TNMStadia_id = stad.TNMStadia_id
			left join nsi.TNMCategory catT   on stad.TNMCategory_tid = catT.TNMCategory_id
			left join nsi.TNMCategory catN   on stad.TNMCategory_nid = catN.TNMCategory_id
			left join nsi.TNMCategory catM   on stad.TNMCategory_mid = catM.TNMCategory_id
			left join nsi.TNMCategory catG   on stad.TNMCategory_aid = catG.TNMCategory_id
			left join nsi.TNMStage stage   on stad.TNMStage_id = stage.TNMStage_id

			left join v_OnkoLesionSide ols   on mrbo.OnkoLesionSide_id = ols.OnkoLesionSide_id
			left join v_OnkoDiag od   on mrbo.OnkoDiag_mid = od.OnkoDiag_id

			left join dbo.v_TumorStageLinkNsi stagel2   on mrbo.TumorStage_id = stagel2.TumorStage_id 
			left join nsi.v_TumorStage tstage   on tstage.TumorStage_id = stagel2.TumorStage_nid 

			left join v_OnkoDiagConfType odct   on mrbo.OnkoDiagConfType_id = odct.OnkoDiagConfType_id
			left join YesNo yn   on mrbo.MorbusOnko_IsMainTumor = yn.YesNo_id
			left join v_TumorCircumIdentType tcit   on mrbo.TumorCircumIdentType_id = tcit.TumorCircumIdentType_id
			
			left join v_Diag suspdiag   on suspdiag.Diag_id = entity.Diag_spid

	         left join lateral  
	        (select   cl.OnkoClinicGroup_Code, cl.OnkoClinicGroup_Name 
	        from v_MorbusClinicGroupHistory mcgh   
	        inner join v_OnkoClinicGroupLink link   on link.OnkoStatusYearEndType_id = mcgh.OnkoStatusYearEndType_id
			inner join nsi.OnkoClinicGroup cl   on cl.OnkoClinicGroup_id = link.OnkoClinicGroup_id
	        where mcgh.Morbus_id = mrb.Morbus_id and mcgh.MorbusClinicGroupHistory_insDT <= coalesce(entity.Evn_setDT, dbo.tzGetDate()) 
	        order by mcgh.MorbusClinicGroupHistory_insDT desc
	         limit 1 
	        ) clgr  on true 

	         left join lateral 
	        (select    VimisPatientControl_begDate from vimis.VimisPatientControl vpc   
	        where vpc.Person_id = $41 and vpc.VimisType_id = 1
	        order by vpc.VimisPatientControl_begDate 
	         limit 1 
	        ) vpc  on true
