
                
            with ttg (
                TimetableGraf_id,
                TimetableGraf_updDT
            ) as (
                select
                       g.TimetableGraf_id,
                       g.TimetableGraf_updDT
                from
                     v_TimetableGraf_lite g
                     inner join v_MedStaffFact msf on msf.MedStaffFact_id = g.MedStaffFact_id
                where g.TimetableGraf_begTime >= tzgetdate()
                  and g.RecMethodType_id in (1, 3, 14, 15)
                  and g.Person_id is not null
                   and msf.Lpu_id = '2800' 
            ),
            curDT as (select dbo.tzGetDate() as curDate)
            
                
                SELECT 
                   g.TimetableGraf_id as "TimetableGraf_id",
                   g.TimetableGraf_Day as "TimetableGraf_Day",
                   to_char(g.TimetableGraf_begTime, 'DD.MM.YYYY HH24:MI:SS') as "TimetableGraf_begTime",
                   to_char(g.TimetableGraf_updDT, 'DD.MM.YYYY HH24:MI:SS') as "TimetableGraf_updDT",
                   g.TimetableGraf_IsModerated as "TimetableGraf_IsModerated",
                   l.Lpu_Nick as "Lpu_Nick",
                   luas.KLStreet_Name||', '||lua.Address_House as "LpuUnit_Address",
                   lu.LpuUnit_Name as "LpuUnit_Name",
                   msf.MedPersonal_FIO as "MedPersonFullName",
                   ls.LpuSectionProfile_Name as "LpuSectionProfile_Name",
                   substring(lr.LpuRegion_Name , 1, length(lr.LpuRegion_Name)) as "MedLpuRegion_Name",
                   LPR.LpuRegion_Name as "LpuRegion_Name",
                   null as "LpuRegion_Name_Pr",
                   lu.LpuUnit_id as "LpuUnit_id",
                   msf.MedStaffFact_id as "MedStaffFact_id",
                   p.Person_id as "Person_id",
                   
            p.Person_surname||' '||p.Person_firname||' '||coalesce(p.Person_secname, '') as "PersonFullName",
            case
                when p.Person_Phone is null and p.PersonInfo_InternetPhone is null then ''
                when p.Person_Phone is not null and p.PersonInfo_InternetPhone is not null then 'В нашей базе: '||p.Person_Phone||'<br />Указано человеком: '||coalesce(p.PersonInfo_InternetPhone, '')
                when p.Person_Phone is not null then 'В нашей базе: '||p.Person_Phone
                when p.PersonInfo_InternetPhone is not null then 'Указано человеком: '||coalesce(p.PersonInfo_InternetPhone, '')
            end as "Person_Phone",
            to_char(p.Person_BirthDay, 'DD.MM.YYYY') as "Person_BirthDay",
            rtrim(replace(coalesce(a1.Address_Address, ''), 'РОССИЯ, ПЕРМСКИЙ КРАЙ, ', '')) as "PAddress_Address",
            rtrim(replace(coalesce(a.Address_Address, ''), 'РОССИЯ, ПЕРМСКИЙ КРАЙ, ', ''))  as "UAddress_Address",
            case when a1.Address_id is not null
                then rtrim(a1.Address_House)
                else rtrim(a.Address_House)
            end as "Address_House",
            case when a1.Address_id is not null
                then a1.KLStreet_id
                else a.KLStreet_id
            end as "KLStreet_id",
        
                   l.Lpu_id as "Lpu_id",
                   case when coalesce(p.Polis_endDate, (select curDate from curDT)) >= (select curDate from curDT) then '' else 'true' end as "Person_IsBDZ",
                   p.Lpu_id as "PrikLpu_id",
                   p.Lpu_Nick as "PrikLpu_Nick",
                   null as "MedpersonalDay_Descr",
                   null as "MedstaffFact_Descr",
                   p.Server_pid as "Server_pid",
                   coalesce(g.pmUser_updID, -1) as "pmUser_updID"
                   
                FROM
                
                ttg
                inner join v_TimetableGraf_lite g on g.TimetableGraf_id = ttg.TimetableGraf_id
                inner join v_Person_ER p on p.Person_id = g.Person_id
                inner join v_MedStaffFact_ER msf on msf.MedStaffFact_id = g.MedStaffFact_id
                left join v_LpuSection_ER ls on ls.LpuSection_id = msf.LpuSection_id
                left join v_LpuUnit_ER lu on lu.LpuUnit_id = ls.LpuUnit_id
                left join Address lua on lu.Address_id = lua.Address_id
                left join KLStreet luas on lua.KLStreet_id = luas.KLStreet_id
                left join v_Lpu l on l.Lpu_id = lu.Lpu_id
                left join lateral (
                    select LpuRegion_id
                    from v_PersonCard_all
                    where Person_id = p.Person_id
                      and PersonCard_endDate is null
                    order by case when LpuAttachType_id = 4 and Lpu_id = p.Lpu_id then 0 else LpuAttachType_id end, PersonCard_begDate
                    limit 1
                ) as PersonCard on true
                left join v_LpuRegion LPR on LPR.LpuRegion_id = PersonCard.LpuRegion_id
                left join lateral (
                    select (
                             select string_agg(LpuRegion_Name, ', ' order by cast (LpuRegion_Name as integer))
                             from v_MedStaffRegion t2
                                  left join v_LpuRegion lr2 on t2.LpuRegion_id = lr2.LpuRegion_id
                             where t1.MedPersonal_id = t2.MedPersonal_id
                            ) as LpuRegion_Name
                     from v_MedStaffRegion t1
                          left join v_LpuRegion lr1 on t1.LpuRegion_id = lr1.LpuRegion_id
                     where t1.MedPersonal_id = msf.MedPersonal_id
                     group by t1.MedPersonal_id
                ) lr on true
                left outer join Address a on p.UAddress_id = a.Address_id
                left outer join Address a1 on p.PAddress_id = a1.Address_id
                left outer join KLStreet pas on a.KLStreet_id = pas.KLStreet_id
                left outer join KLStreet pas1 on a1.KLStreet_id = pas1.KLStreet_id
                left join MedpersonalDay mpd on mpd.MedStaffFact_id = g.MedStaffFact_id and mpd.Day_id = g.TimetableGraf_Day
                
                 WHERE 
                (1 = 1) and p.Person_SurName ilike 'ЗЕЛЕНЦОВА%'  and p.Person_FirName ilike 'КРИСТИНА%'  and p.Person_SecName ilike 'МАКСИМОВНА%'  and (g.TimetableGraf_IsModerated is null or g.TimetableGraf_IsModerated = 2)  and msf.Lpu_id = '2800' 
                
                
                        ORDER BY
                            
                ttg.TimetableGraf_updDT
                
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
