
            select
                dcm2.DrugComplexMnn_id as "DrugComplexMnn_sort",
                dcm1.DrugComplexMnn_id as "DrugComplexMnn_id",
                to_char(min(E.Evn_setDate)::date, 'DD.MM.YYYY') as "AntagonistDrug_AppDate",
                case
                    when EA.EvnClass_SysNick = 'EvnPS' then ' КВС №'
                    when EA.EvnClass_SysNick = 'EvnPL' then ' ТАП №'
                end as "Evn_Name",
                case
                    when EA.EvnClass_SysNick = 'EvnPS' then ES.EvnPS_NumCard
                    when EA.EvnClass_SysNick = 'EvnPL' then EP.EvnPL_NumCard
                end as "Evn_NumCard",
                date_part ('day' , max(E.Evn_setDate) - cast('2022-03-20' as date) ) as "AntagonistDrug_RemainingDay",
                dcmn1.DrugComplexMnnName_Name as "AntagonistDrug_Name",
                case when E.Evn_pid = '70101206983052' then 1 else 0 end as "thisEvn",
                ll.DESCRIPTION as "AntagonistDrug_Description",
                ll.RECOMMENDATION as "AntagonistDrug_Recommendation",
                ll.BREAKTIME as "AntagonistDrug_BreakTime"
            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 = '574016'
                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 = '250504'
                and E.EvnClass_SysNick = 'EvnPrescrTreat'
            group by
                dcm2.DrugComplexMnn_id,
                dcm1.DrugComplexMnn_id,
                EA.EvnClass_SysNick,
                E.Evn_rid,
                EA.Evn_setDate,
                dcm1.DrugComplexMnn_RusName,
                dcm2.DrugComplexMnn_RusName,
                EP.EvnPL_NumCard,
                ES.EvnPS_NumCard,
                E.Evn_pid,
                ll.DESCRIPTION,
                ll.RECOMMENDATION,
                ll.BREAKTIME,
                dcmn1.DrugComplexMnnName_Name

            union all

            select
                dcm2.DrugComplexMnn_id as "DrugComplexMnn_sort",
                dcm1.DrugComplexMnn_id as "DrugComplexMnn_id",
                '' as "AntagonistDrug_AppDate",
                '' as "Evn_Name",
                '' as "Evn_NumCard",
                1 as "AntagonistDrug_RemainingDay",
                dcmn1.DrugComplexMnnName_Name as "AntagonistDrug_Name",
                3 as "thisEvn",
                ll.DESCRIPTION as "AntagonistDrug_Description",
                ll.RECOMMENDATION as "AntagonistDrug_Recommendation",
                ll.BREAKTIME as "AntagonistDrug_BreakTime"
            from rls.v_DrugComplexMnn dcm1
                inner join rls.v_DrugComplexMnn dcm2 on dcm2.DrugComplexMnn_id = '574016'
                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('574016')
            group by
                dcm2.DrugComplexMnn_id,
                dcm1.DrugComplexMnn_id,
                dcm1.DrugComplexMnn_RusName,
                dcm2.DrugComplexMnn_RusName,
                ll.DESCRIPTION,
                ll.RECOMMENDATION,
                ll.BREAKTIME,
                dcmn1.DrugComplexMnnName_Name

            union all

            select
                dcm2.DrugComplexMnn_id as DrugComplexMnn_sort,
                dcm1.DrugComplexMnn_id,
                '' as AntagonistDrug_AppDate,
                '' as Evn_Name,
                '' as Evn_NumCard,
                1 as AntagonistDrug_RemainingDay,
                dcmn1.DrugComplexMnnName_Name as AntagonistDrug_Name,
                3 as thisEvn,
                ll.DESCRIPTION as AntagonistDrug_Description,
                ll.RECOMMENDATION as AntagonistDrug_Recommendation,
                ll.BREAKTIME as AntagonistDrug_BreakTime
            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 = '574016'
                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
            group by
                dcm2.DrugComplexMnn_id,
                dcm1.DrugComplexMnn_id,
                dcm1.DrugComplexMnn_RusName,
                dcm2.DrugComplexMnn_RusName,
                ll.DESCRIPTION,
                ll.RECOMMENDATION,
                ll.BREAKTIME,
                dcmn1.DrugComplexMnnName_Name
        
