
            select 
              
             EU.EvnUsluga_id as "EvnUsluga_id"
            ,EU.EvnUsluga_pid as "EvnUsluga_pid"
            ,RTRIM(EU.EvnClass_SysNick) as "EvnClass_SysNick"
            ,to_char (EU.EvnUsluga_setDate, 'dd.mm.yyyy') as "EvnUsluga_setDate"
            ,COALESCE(to_char(EU.EvnUsluga_setTime, 'HH24:mi'),'') as "EvnUsluga_setTime"
            ,COALESCE(Usluga.Usluga_Code, UC.UslugaComplex_Code) as "Usluga_Code"
            ,COALESCE(Usluga.Usluga_Name, UC.UslugaComplex_Name) as "Usluga_Name"
            ,ROUND(cast(EU.EvnUsluga_Kolvo as numeric), 2) as "EvnUsluga_Kolvo"
            ,ROUND(cast(EU.EvnUsluga_Price as numeric), 2) as "EvnUsluga_Price"
            ,ROUND(cast(EU.EvnUsluga_Summa as numeric), 2) as "EvnUsluga_Summa"
            ,PT.PayType_id as "PayType_id"
            ,COALESCE(PT.PayType_SysNick, '') as "PayType_SysNick"
            ,UC.UslugaComplex_id as "UslugaComplex_id"
            ,UCA.Usluga_DI_count as "Usluga_DI_count"
            ,UCAA.Usluga_PP_count as "Usluga_PP_count"
        
                ,case when EU.EvnUsluga_IsVizitCode = 2 then 'X' else '' end as "EvnUsluga_IsVizitCode"
                ,case when EUS.EvnUslugaStom_IsAllMorbus = 2 then 'X' else '' end as "EvnUsluga_IsAllMorbus"
                ,case when EUS.EvnUslugaStom_IsMes = 2 then 'X' else '' end as "EvnUsluga_IsMes"
                ,case when MU.MesUsluga_IsNeedUsluga = 2 then 'X' else '' end as "EvnUsluga_IsRequired"
            
                ,ROUND(COALESCE(EU.EvnUsluga_Price, 0), 2) as "EvnUsluga_Price"
                ,ROUND(COALESCE(EU.EvnUsluga_Summa, 0), 2) as "EvnUsluga_Summa"
            
              
            from
              
            v_EvnUsluga EU --on UslugaPid.EvnUsluga_id = EU.Evnusluga_id
            left join lateral (select * from  v_Evn EvnParent where EvnParent.Evn_id = EU.EvnUsluga_pid limit 1) EvnParent on true
            left join lateral (select * from  v_EvnPrescr EP where EP.EvnPrescr_id = EU.EvnPrescr_id limit 1) EP on true
            left join lateral (select * from  v_Usluga Usluga where Usluga.Usluga_id = EU.Usluga_id limit 1) Usluga on true
            left join lateral (select * from  v_UslugaComplex UC where UC.UslugaComplex_id = EU.UslugaComplex_id limit 1) UC on true
            left join lateral (select * from  v_EvnVizit EV where EV.EvnVizit_id = EU.EvnUsluga_pid limit 1) EV on true
            left join lateral (select * from  v_EvnSection ES where ES.EvnSection_id = EU.EvnUsluga_pid limit 1) ES on true
            left join v_PayType PT on PT.PayType_id = EU.PayType_id
            left join lateral (
                select count(*) as Usluga_DI_count from v_UslugaComplexAttribute UCA
                    left join v_UslugaComplexAttributeType UCAT on UCAT.uslugacomplexattributetype_id = UCA.uslugacomplexattributetype_id
                where
                    UCA.UslugaComplex_id = UC.UslugaComplex_id
                    and UCAT.UslugaComplexAttributeType_code = 290
                group by
                    UCA.UslugaComplex_id
            ) UCA on true
            left join lateral (
                select count(*) as Usluga_PP_count from v_UslugaComplexAttribute UCAA
                left join v_UslugaComplexAttributeType UCAT on UCAT.uslugacomplexattributetype_id = UCAA.uslugacomplexattributetype_id
                where
                    UCAA.UslugaComplex_id = UC.UslugaComplex_id
                    and UCAT.UslugaComplexAttributeType_Code = 291
                group by
                    UCAA.UslugaComplex_id
            ) UCAA on true
        
                left join v_EvnUslugaStom EUS on EUS.EvnUslugaStom_id = EU.EvnUsluga_id
                left join v_EvnDiagPLStom EDPLS on EDPLS.EvnDiagPLStom_id = EUS.EvnDiagPLStom_id
                LEFT JOIN LATERAL (
                    select MesUsluga_IsNeedUsluga
                    from v_MesUsluga
                    where Mes_id = EDPLS.Mes_id
                        and UslugaComplex_id = EU.UslugaComplex_id
                    limit 1
                ) MU ON true

             left join v_UslugaComplexTariff UCT on UCT.UslugaComplexTariff_id = EU.UslugaComplexTariff_id
            where
              (1=1) and (EvnParent.EvnClass_SysNick <> 'EvnUslugaPar' or EvnParent.EvnClass_SysNick is null)
and COALESCE(Usluga.Usluga_Code, UC.UslugaComplex_Code)='A16.07.047'
                      --  and EU.Person_id = '719275'

