
                
                
                SELECT 
                ESP.EvnPL_id as "Evn_id",
                'ТАП' as "EvnStick_pidType",
                ESP.EvnPL_NumCard as "EvnStick_pidNum",
                coalesce(PS.Person_SurName,'') || ' ' || coalesce(PS.Person_FirName,'') || ' ' || coalesce(PS.Person_SecName,'') as "Person_FIO",
                coalesce(MP.Person_SurName,'') || ' ' || coalesce(MP.Person_FirName,'') || ' ' || coalesce(MP.Person_SecName,'') as "MedPersonal_FIO",
                to_char(ESP.EvnPL_setDate, 'dd.mm.yyyy') as "EvnStick_pidBegDate",
                to_char(ESP.EvnPL_disDate, 'dd.mm.yyyy') as "EvnStick_pidEndDate",
                coalesce(LS.LpuSection_FullName,'') as "LpuSection_Name",
                case when (
                        ESB.EvnStickBase_id is null
                        and EL.Evn_lid is null)
                    then 'false'
                    else 'true'
                end as "HasEvnStick",
                coalesce(PJ.Org_id, null) as "JobOrg_id",
                PS.Person_id as "Person_id",
                ESP.PersonEvn_id as "PersonEvn_id",
                ESP.Server_id as "Server_id",
                coalesce(to_char(PS.Person_BirthDay, 'dd.mm.yyyy'), '') as "Person_Birthday",
                coalesce(RTRIM(PP.Post_Name), '') as "Person_Post",
                coalesce(PS.Person_SurName,'') as "Person_SurName",
                coalesce(PS.Person_FirName,'') as "Person_FirName",
                coalesce(PS.Person_SecName,'') as "Person_SecName"
            
                FROM
                
                v_EvnPL ESP
                inner join v_PersonState PS on PS.Person_id = ESP.Person_id

                left join lateral (
                    select ESBt.EvnStickBase_id, ESBt.EvnStickBase_IsFSS
                    from v_EvnStickBase ESBt
                    where ESBt.EvnStickBase_mid = ESP.EvnPL_id
                       or ESBt.EvnStickBase_rid = ESP.EvnPL_id
                    limit 1
                ) ESB on true

                left join lateral (
                    select ELt.Evn_lid
                    from v_EvnLink ELt
                    where ELt.Evn_id = ESP.EvnPL_id
                    limit 1
                ) EL on true

                --left join StickType ST on ST.StickType_id = ES.StickType_id
                
                
                left join v_MedPersonal MP on MP.MedPersonal_id = ESP.MedPersonal_id and MP.Lpu_id = ESP.Lpu_id
                left join v_LpuSection LS on LS.LpuSection_id = ESP.LpuSection_id
                left join v_Job Job on Job.Job_id = PS.Job_id
                left join Org PJ on PJ.Org_id = Job.Org_id
                left join Post PP on PP.Post_id = Job.Post_id
             WHERE 
                ESP.Lpu_id = '2722'
                 and cast(ESP.EvnPL_setDT as date) >= cast('2022-03-18' as date) and cast(ESP.EvnPL_setDT as date) <= cast('2022-03-18' as date)
                
                
                        ORDER BY
                            
                PS.Person_SurName,
                PS.Person_FirName,
                PS.Person_SecName
                
                OFFSET 0 ROWS
                FETCH NEXT 50 ROWS ONLY
                
