
			select
				dcm.DrugComplexMnn_RusName as "Drug_Name",
				( 
					select 
						string_agg(ad.AntagonistDrug_Names,', ') 
					from (
						select 
						dcm1.DrugComplexMnn_RusName || 
						case 
							when E.Evn_pid = '500801005615243' then ', назначенным ранее, ' 
							else
								case 
									when EA.EvnClass_SysNick = 'EvnPS' then ' (КВС №' || ES.EvnPS_NumCard || ' от ' || to_char(EA.Evn_setDate::date, 'DD.MM.YYYY') || '), '
									when EA.EvnClass_SysNick = 'EvnPL' then ' (ТАП №' || EP.EvnPL_NumCard || ' от ' || to_char(EA.Evn_setDate::date, 'DD.MM.YYYY') || '), '
								end
						end as AntagonistDrug_Names
						from v_Evn E 
							inner join v_EvnPrescrTreat EPT on EPT.EvnPrescrTreat_id = E.Evn_id
							inner join v_EvnPrescrTreatDrug EPTD on EPTD.EvnPrescrTreat_id = EPT.EvnPrescrTreat_id
							inner join rls.v_DrugComplexMnn dcm1 on dcm1.DrugComplexMnn_id = EPTD.DrugComplexMnn_id
							inner join rls.v_DrugComplexMnn dcm2 on dcm2.DrugComplexMnn_id = '446776'
							inner join rls.v_DrugComplexMnnName dcmn1 on dcmn1.DrugComplexMnnName_id = dcm1.DrugComplexMnnName_id
							inner join rls.v_DrugComplexMnnName dcmn2 on dcmn2.DrugComplexMnnName_id = dcm2.DrugComplexMnnName_id
							left join v_Evn EA on ea.Evn_id = E.Evn_rid
							left join v_EvnPL EP on EP.EvnPL_id = ea.Evn_id
							left join v_EvnPS ES on ES.EvnPS_id = ea.Evn_id
							inner join rls.v_LS_LINK ll on (
								(
									( ll.ACTMATTERS_G1ID = dcmn1.ACTMATTERS_ID or ll.TRADENAMES_G1ID = dcmn1.TRADENAMES_ID ) 
										and ( ll.ACTMATTERS_G2ID = dcmn2.ACTMATTERS_ID or ll.TRADENAMES_G2ID = dcmn2.TRADENAMES_ID )
								) or (
									( ll.ACTMATTERS_G1ID = dcmn2.ACTMATTERS_ID or ll.TRADENAMES_G1ID = dcmn2.TRADENAMES_ID ) 
										and ( ll.ACTMATTERS_G2ID = dcmn1.ACTMATTERS_ID or ll.TRADENAMES_G2ID = dcmn1.TRADENAMES_ID )
								)
							) and ll.LS_EFFECT_ID in (3, 4)
							left join rls.v_LS_EFFECT le on le.LS_EFFECT_ID = ll.LS_EFFECT_ID
						where 
							E.Person_id = '5404660'
							and E.EvnClass_SysNick = 'EvnPrescrTreat'
							and cast(E.Evn_setDate as date) = cast('2022-01-22' as date)
						group by 
							EA.EvnClass_SysNick,
							E.Evn_rid,
							EA.Evn_setDate,
							dcm1.DrugComplexMnn_RusName,
							dcm2.DrugComplexMnn_RusName,
							EP.EvnPL_NumCard,
							ES.EvnPS_NumCard,
							E.Evn_pid
							
						union all
						
						select 
							dcm1.DrugComplexMnn_RusName || ', назначенным ранее, ' as AntagonistDrug_Names
						from rls.v_DrugComplexMnn dcm1
							inner join rls.v_DrugComplexMnn dcm2 on dcm2.DrugComplexMnn_id = '446776'
							inner join rls.v_DrugComplexMnnName dcmn1 on dcmn1.DrugComplexMnnName_id = dcm1.DrugComplexMnnName_id
							inner join rls.v_DrugComplexMnnName dcmn2 on dcmn2.DrugComplexMnnName_id = dcm2.DrugComplexMnnName_id
							inner join rls.v_LS_LINK ll on (
								(
									( ll.ACTMATTERS_G1ID = dcmn1.ACTMATTERS_ID or ll.TRADENAMES_G1ID = dcmn1.TRADENAMES_ID ) 
										and ( ll.ACTMATTERS_G2ID = dcmn2.ACTMATTERS_ID or ll.TRADENAMES_G2ID = dcmn2.TRADENAMES_ID )
								) or (
									( ll.ACTMATTERS_G1ID = dcmn2.ACTMATTERS_ID or ll.TRADENAMES_G1ID = dcmn2.TRADENAMES_ID ) 
										and ( ll.ACTMATTERS_G2ID = dcmn1.ACTMATTERS_ID or ll.TRADENAMES_G2ID = dcmn1.TRADENAMES_ID )
								)
							) and ll.LS_EFFECT_ID in (3, 4)
						where 
							dcm1.DrugComplexMnn_id in('0')
													
						union all
						
						select 
							dcm1.DrugComplexMnn_RusName ||  ', назначенным ранее, ' as AntagonistDrug_Names
						from dbo.v_PacketPrescr pp
							inner join dbo.v_PacketPrescrList ppl on ppl.PacketPrescr_id = pp.PacketPrescr_id
							inner join dbo.v_PacketPrescrTreat ppt on ppt.PacketPrescrList_id = ppl.PacketPrescrList_id
							left join dbo.v_PacketPrescrTreatDrug pptd on pptd.PacketPrescrTreat_id = ppt.PacketPrescrTreat_id
							left join rls.v_DrugComplexMnnName MnnName on MnnName.ACTMATTERS_id = pptd.ACTMATTERS_ID
							left join rls.v_Drug Drug on Drug.Drug_id = pptd.Drug_id
							inner join rls.v_DrugComplexMnn dcm1 on dcm1.DrugComplexMnn_id = coalesce(pptd.DrugComplexMnn_id,Drug.DrugComplexMnn_id)
							inner join rls.v_DrugComplexMnn dcm2 on dcm2.DrugComplexMnn_id = '446776'
							inner join rls.v_DrugComplexMnnName dcmn1 on dcmn1.DrugComplexMnnName_id = dcm1.DrugComplexMnnName_id
							inner join rls.v_DrugComplexMnnName dcmn2 on dcmn2.DrugComplexMnnName_id = dcm2.DrugComplexMnnName_id
							inner join rls.v_LS_LINK ll on (
								(
									( ll.ACTMATTERS_G1ID = dcmn1.ACTMATTERS_ID or ll.TRADENAMES_G1ID = dcmn1.TRADENAMES_ID ) 
										and ( ll.ACTMATTERS_G2ID = dcmn2.ACTMATTERS_ID or ll.TRADENAMES_G2ID = dcmn2.TRADENAMES_ID )
								) or (
									( ll.ACTMATTERS_G1ID = dcmn2.ACTMATTERS_ID or ll.TRADENAMES_G1ID = dcmn2.TRADENAMES_ID ) 
										and ( ll.ACTMATTERS_G2ID = dcmn1.ACTMATTERS_ID or ll.TRADENAMES_G2ID = dcmn1.TRADENAMES_ID )
								)
							) and ll.LS_EFFECT_ID in (3, 4)
						where 
							pp.PacketPrescr_id = 0
							and ppl.PrescriptionType_id = 5
						) as ad
				) as "AntagonistDrug_Names"
				from rls.v_DrugComplexMnn dcm
				inner join rls.v_DrugComplexMnnName dcmn on dcmn.DrugComplexMnnName_id = dcm.DrugComplexMnnName_id
				where 
					dcm.DrugComplexMnn_id = '446776'
				limit 1
		
