
                
            with mv as (
                select
                localtimestamp as dt
            )
            
                
                SELECT 
                -- TOP SL, FIXDATE 18.05.2023, JIRA PROMEDWEB-148004
        
            PR.PersonRegister_id as "PersonRegister_id",
            MO.MorbusPalliat_id as "MorbusPalliat_id",
            PRT.PersonRegisterType_SysNick as "PersonRegisterType_SysNick",
            rtrim(MT.MorbusType_SysNick) as "MorbusType_SysNick",
            PR.Diag_id as "Diag_id",
            PR.Lpu_iid as "Lpu_iid",
            PR.MedPersonal_iid as "MedPersonal_iid",
            PR.EvnNotifyBase_id as "EvnNotifyBase_id",
            PR.Morbus_id as "Morbus_id",
            PS.Person_id as "Person_id",
            PS.Server_id as "Server_id",
            PS.PersonEvn_id as "PersonEvn_id",
            RTRIM(PS.Person_SurName) as "Person_Surname",
            RTRIM(PS.Person_FirName) as "Person_Firname",
            RTRIM(PS.Person_SecName) as "Person_Secname",
            to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday",
            to_char(PS.Person_closeDT, 'DD.MM.YYYY') as "Person_closeDT",
            Lpu.Lpu_Nick as "Lpu_Nick",
            LpuIns.Lpu_Nick as "Lpu_insNick",
            Diag.diag_FullName as "Diag_Name",
            PROUT.PersonRegisterOutCause_id as"PersonRegisterOutCause_id",
            PROUT.PersonRegisterOutCause_Name as"PersonRegisterOutCause_Name",
            to_char(PR.PersonRegister_setDate, 'DD.MM.YYYY') as "PersonRegister_setDate",
            to_char(PR.PersonRegister_disDate, 'DD.MM.YYYY') as "PersonRegister_disDate",
            MPExclude.MedPersonal_FIO as "MedPersonal_FIO"
        
            
                FROM
                
         dbo.v_PersonState PS 
            inner join v_PersonRegisterType PRT on PRT.PersonRegisterType_SysNick = 'palliat'
            inner join v_PersonRegister PR on PR.Person_id = PS.Person_id and PR.PersonRegisterType_id = PRT.PersonRegisterType_id
            left join v_MorbusPalliat MO on MO.Morbus_id = PR.Morbus_id
            left join v_PersonRegisterOutCause PROUT on PROUT.PersonRegisterOutCause_id = PR.PersonRegisterOutCause_id
            left join lateral(
                    select
                        *
                    from
                        v_PersonCard_all t
                    where
                        t.Person_id = PS.Person_id and
                        t.LpuAttachType_id = 1
                    order by
                        PersonCard_begDate desc
                    limit 1
            ) as PC on true
            left join v_Lpu Lpu on Lpu.Lpu_id = PC.Lpu_id
            left join v_Lpu LpuIns on LpuIns.Lpu_id = pr.Lpu_iid
            left join v_Diag Diag on Diag.Diag_id = PR.Diag_id
            left join v_MorbusType MT on MT.MorbusType_id = PR.MorbusType_id
            left join lateral (
                select Person_Fin as MedPersonal_FIO
                from v_MedPersonal
                where MedPersonal_id = PR.MedPersonal_did
                limit 1
            ) MPExclude on true
            left join lateral (
                select MPE.*
                from v_Evn E
                    inner join v_MorbusPalliatEvn MPE on E.Evn_id = MPE.Evn_id
                where E.Person_id = PS.Person_id
                Order by E.Evn_id desc
                limit 1
            ) MPE on true
        
                         WHERE 
                        (1 = 1)
                        and exists(
                            select 1
                            from v_Person_all PStmp
                            where PStmp.Person_id = PS.Person_id
                             and lower(PStmp.Person_SurName) like lower('королева')||'%'
                    ) 
                        
                
                        ORDER BY
                            
                        PR.PersonRegister_setDate DESC,
                        PR.PersonRegister_id
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
