
            with cte as (select dbo.tzGetDate() as curdate)
            
			select distinct
				l.Lpu_id as "Lpu_id",
				l.Lpu_Phone as "Lpu_Phone",
				lr.LpuRegion_id as "LpuRegion_id",
                lr.LpuRegion_Name as "LpuRegion_Name",
                lrt.LpuRegionType_Name as "LpuRegionType_Name",
				lrt.LpuRegionType_SysNick as "LpuRegionType_SysNick",
                l.Lpu_Name as "Lpu_Name",
                l.Lpu_Nick as "Lpu_Nick",
                MedStaffFact_List as "MedStaffFact_List",
                lrs.LpuRegionStreet_HouseSet as "LpuRegionStreet_HouseSet",
			    dshi.DataStorage_Value as "homevizit_isallowed",
			    dshnp.DataStorage_Value as "homevizit_nmp_phone",
			    dshp.DataStorage_Value as "homevizit_phone",
			    dshsp.DataStorage_Value as "homevizit_smp_phone"
			from LpuRegionStreet lrs
			    
				left join v_LpuRegion lr on lr.LpuRegion_id = lrs.LpuRegion_id
				left join v_LpuRegionType lrt on lr.LpuRegionType_id = lrt.LpuRegionType_id
				left join lateral ( 
					select 
						ERTerr_id
					from ERTerr Terr
					where
						(
							((lrs.KLCountry_id = Terr.KLCountry_id) or (Terr.KLCountry_id is null)) and
							((lrs.KLRGN_id = Terr.KLRGN_id) or (Terr.KLRGN_id is null)) and
							((lrs.KLSubRGN_id = Terr.KLSubRGN_id) or (Terr.KLSubRGN_id is null)) and
							((lrs.KLCity_id = Terr.KLCity_id) or (Terr.KLCity_id is null)) and
							((lrs.KLTown_id = Terr.KLTown_id) or (Terr.KLTown_id is null))
						)
					LIMIT 1
				) Terr on true
				inner join lateral (
					select 
						( select String_agg(cast(msf2.MedStaffFact_id as varchar) || '=' || msf2.Person_FIO|| ';' || cast(msf2.LpuUnit_id as varchar)|| ';' ||cast(msf2.Lpu_id as varchar)|| ';' || cast(ls2.LpuSectionProfile_id as varchar) || '|', '') as "data()"
							from v_MedStaffRegion msr2 
							inner join v_MedStaffFact msf2 on msr2.MedStaffFact_id = msf2.MedStaffFact_id and 
				                cast(coalesce(msr2.MedStaffRegion_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date) and 
				                cast(coalesce(msf2.WorkData_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date)
							inner join v_LpuSection ls2 on ls2.LpuSection_id = msf2.LpuSection_id
							inner join v_LpuUnit lu2 on ls2.LpuUnit_id = lu2.LpuUnit_id and coalesce(lu2.LpuUnit_IsEnabled, 1) = 2
								inner join v_MedSpecOms mso2 on mso2.MedSpecOms_id = msf2.MedSpecOms_id and coalesce(mso2.MedSpecOms_IsPortalPost, 2) = 2 and mso2.MedSpecOms_PortalName is not null 
							where 
								msr2.LpuRegion_id = msr.LpuRegion_id and msr.Lpu_id = msr2.Lpu_id
							
						) as MedStaffFact_List,
						msf.LpuUnit_id,
						ls.LpuSectionProfile_id
					from v_MedStaffRegion msr
					inner join v_MedStaffFact msf on msf.MedStaffFact_id = msr.MedStaffFact_id and 
				        cast(coalesce(msr.MedStaffRegion_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date) and
				        cast(coalesce(msf.WorkData_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date)
					inner join v_LpuSection ls on ls.LpuSection_id = msf.LpuSection_id
					 	inner join v_MedSpecOms mso on mso.MedSpecOms_id = msf.MedSpecOms_id and coalesce(mso.MedSpecOms_IsPortalPost, 2) = 2 and mso.MedSpecOms_PortalName is not null 
					where
						msf.Lpu_id = lr.Lpu_id
						and lr.LpuRegion_id = msr.LpuRegion_id
				) msf on true
				left join v_LpuUnit lu on lu.LpuUnit_id = msf.LpuUnit_id
				left join v_Lpu l on lu.Lpu_id = l.Lpu_id
				left join v_DataStorage dshi on l.Lpu_id = dshi.Lpu_id and dshi.DataStorage_Name = 'homevizit_isallowed'
				left join v_DataStorage dshnp on l.Lpu_id = dshnp.Lpu_id and dshnp.DataStorage_Name = 'homevizit_nmp_phone'
				left join v_DataStorage dshp on l.Lpu_id = dshp.Lpu_id and dshp.DataStorage_Name = 'homevizit_phone'
				left join v_DataStorage dshsp on l.Lpu_id = dshsp.Lpu_id and dshsp.DataStorage_Name = 'homevizit_smp_phone'
				left join LpuSectionProfile lsp on msf.LpuSectionProfile_id = lsp.LpuSectionProfile_id
			where lu.LpuUnitType_id = 2
                and l.Lpu_id = lr.Lpu_id
				and COALESCE(l.Lpu_IsTest, 1) = 1
				and (l.Lpu_endDate is null or l.Lpu_endDate >= (select curdate from cte))
				
             and lrs.KLStreet_id = '520101000088653'
            union
            
			select distinct
				l.Lpu_id as "Lpu_id",
				l.Lpu_Phone as "Lpu_Phone",
				lr.LpuRegion_id as "LpuRegion_id",
                lr.LpuRegion_Name as "LpuRegion_Name",
                lrt.LpuRegionType_Name as "LpuRegionType_Name",
				lrt.LpuRegionType_SysNick as "LpuRegionType_SysNick",
                l.Lpu_Name as "Lpu_Name",
                l.Lpu_Nick as "Lpu_Nick",
                MedStaffFact_List as "MedStaffFact_List",
                lrs.LpuRegionStreet_HouseSet as "LpuRegionStreet_HouseSet",
			    dshi.DataStorage_Value as "homevizit_isallowed",
			    dshnp.DataStorage_Value as "homevizit_nmp_phone",
			    dshp.DataStorage_Value as "homevizit_phone",
			    dshsp.DataStorage_Value as "homevizit_smp_phone"
			from LpuRegionStreet lrs
			    left join LpuRegionStreet lrs2 on lrs2.KLTown_id = lrs.KLTown_id and lrs2.LpuRegionStreet_IsAll = 2
				left join v_LpuRegion lr on lr.LpuRegion_id = lrs2.LpuRegion_id
				left join v_LpuRegionType lrt on lr.LpuRegionType_id = lrt.LpuRegionType_id
				left join lateral ( 
					select 
						ERTerr_id
					from ERTerr Terr
					where
						(
							((lrs2.KLCountry_id = Terr.KLCountry_id) or (Terr.KLCountry_id is null)) and
							((lrs2.KLRGN_id = Terr.KLRGN_id) or (Terr.KLRGN_id is null)) and
							((lrs2.KLSubRGN_id = Terr.KLSubRGN_id) or (Terr.KLSubRGN_id is null)) and
							((lrs2.KLCity_id = Terr.KLCity_id) or (Terr.KLCity_id is null)) and
							((lrs2.KLTown_id = Terr.KLTown_id) or (Terr.KLTown_id is null))
						)
					LIMIT 1
				) Terr on true
				inner join lateral (
					select 
						( select String_agg(cast(msf2.MedStaffFact_id as varchar) || '=' || msf2.Person_FIO|| ';' || cast(msf2.LpuUnit_id as varchar)|| ';' ||cast(msf2.Lpu_id as varchar)|| ';' || cast(ls2.LpuSectionProfile_id as varchar) || '|', '') as "data()"
							from v_MedStaffRegion msr2 
							inner join v_MedStaffFact msf2 on msr2.MedStaffFact_id = msf2.MedStaffFact_id and 
				                cast(coalesce(msr2.MedStaffRegion_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date) and 
				                cast(coalesce(msf2.WorkData_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date)
							inner join v_LpuSection ls2 on ls2.LpuSection_id = msf2.LpuSection_id
							inner join v_LpuUnit lu2 on ls2.LpuUnit_id = lu2.LpuUnit_id and coalesce(lu2.LpuUnit_IsEnabled, 1) = 2
								inner join v_MedSpecOms mso2 on mso2.MedSpecOms_id = msf2.MedSpecOms_id and coalesce(mso2.MedSpecOms_IsPortalPost, 2) = 2 and mso2.MedSpecOms_PortalName is not null 
							where 
								msr2.LpuRegion_id = msr.LpuRegion_id and msr.Lpu_id = msr2.Lpu_id
							
						) as MedStaffFact_List,
						msf.LpuUnit_id,
						ls.LpuSectionProfile_id
					from v_MedStaffRegion msr
					inner join v_MedStaffFact msf on msf.MedStaffFact_id = msr.MedStaffFact_id and 
				        cast(coalesce(msr.MedStaffRegion_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date) and
				        cast(coalesce(msf.WorkData_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date)
					inner join v_LpuSection ls on ls.LpuSection_id = msf.LpuSection_id
					 	inner join v_MedSpecOms mso on mso.MedSpecOms_id = msf.MedSpecOms_id and coalesce(mso.MedSpecOms_IsPortalPost, 2) = 2 and mso.MedSpecOms_PortalName is not null 
					where
						msf.Lpu_id = lr.Lpu_id
						and lr.LpuRegion_id = msr.LpuRegion_id
				) msf on true
				left join v_LpuUnit lu on lu.LpuUnit_id = msf.LpuUnit_id
				left join v_Lpu l on lu.Lpu_id = l.Lpu_id
				left join v_DataStorage dshi on l.Lpu_id = dshi.Lpu_id and dshi.DataStorage_Name = 'homevizit_isallowed'
				left join v_DataStorage dshnp on l.Lpu_id = dshnp.Lpu_id and dshnp.DataStorage_Name = 'homevizit_nmp_phone'
				left join v_DataStorage dshp on l.Lpu_id = dshp.Lpu_id and dshp.DataStorage_Name = 'homevizit_phone'
				left join v_DataStorage dshsp on l.Lpu_id = dshsp.Lpu_id and dshsp.DataStorage_Name = 'homevizit_smp_phone'
				left join LpuSectionProfile lsp on msf.LpuSectionProfile_id = lsp.LpuSectionProfile_id
			where lu.LpuUnitType_id = 2
                and l.Lpu_id = lr.Lpu_id
				and COALESCE(l.Lpu_IsTest, 1) = 1
				and (l.Lpu_endDate is null or l.Lpu_endDate >= (select curdate from cte))
				
             and lrs.KLStreet_id = '520101000088653'
            union
            
			select distinct
				l.Lpu_id as "Lpu_id",
				l.Lpu_Phone as "Lpu_Phone",
				lr.LpuRegion_id as "LpuRegion_id",
                lr.LpuRegion_Name as "LpuRegion_Name",
                lrt.LpuRegionType_Name as "LpuRegionType_Name",
				lrt.LpuRegionType_SysNick as "LpuRegionType_SysNick",
                l.Lpu_Name as "Lpu_Name",
                l.Lpu_Nick as "Lpu_Nick",
                MedStaffFact_List as "MedStaffFact_List",
                lrs.LpuRegionStreet_HouseSet as "LpuRegionStreet_HouseSet",
			    dshi.DataStorage_Value as "homevizit_isallowed",
			    dshnp.DataStorage_Value as "homevizit_nmp_phone",
			    dshp.DataStorage_Value as "homevizit_phone",
			    dshsp.DataStorage_Value as "homevizit_smp_phone"
			from LpuRegionStreet lrs
			    left join LpuRegionStreet lrs2 on lrs2.KLTown_id = lrs.KLTown_id and lrs2.LpuRegionStreet_IsAll = 2
				left join v_LpuRegion lr on lr.LpuRegion_id = lrs2.LpuRegion_id
				left join v_LpuRegionType lrt on lr.LpuRegionType_id = lrt.LpuRegionType_id
				left join lateral ( 
					select 
						ERTerr_id
					from ERTerr Terr
					where
						(
							((lrs2.KLCountry_id = Terr.KLCountry_id) or (Terr.KLCountry_id is null)) and
							((lrs2.KLRGN_id = Terr.KLRGN_id) or (Terr.KLRGN_id is null)) and
							((lrs2.KLSubRGN_id = Terr.KLSubRGN_id) or (Terr.KLSubRGN_id is null)) and
							((lrs2.KLCity_id = Terr.KLCity_id) or (Terr.KLCity_id is null)) and
							((lrs2.KLTown_id = Terr.KLTown_id) or (Terr.KLTown_id is null))
						)
					LIMIT 1
				) Terr on true
				inner join lateral (
					select 
						( select String_agg(cast(msf2.MedStaffFact_id as varchar) || '=' || msf2.Person_FIO|| ';' || cast(msf2.LpuUnit_id as varchar)|| ';' ||cast(msf2.Lpu_id as varchar)|| ';' || cast(ls2.LpuSectionProfile_id as varchar) || '|', '') as "data()"
							from v_MedStaffRegion msr2 
							inner join v_MedStaffFact msf2 on msr2.MedStaffFact_id = msf2.MedStaffFact_id and 
				                cast(coalesce(msr2.MedStaffRegion_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date) and 
				                cast(coalesce(msf2.WorkData_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date)
							inner join v_LpuSection ls2 on ls2.LpuSection_id = msf2.LpuSection_id
							inner join v_LpuUnit lu2 on ls2.LpuUnit_id = lu2.LpuUnit_id and coalesce(lu2.LpuUnit_IsEnabled, 1) = 2
								inner join v_MedSpecOms mso2 on mso2.MedSpecOms_id = msf2.MedSpecOms_id and coalesce(mso2.MedSpecOms_IsPortalPost, 2) = 2 and mso2.MedSpecOms_PortalName is not null 
							where 
								msr2.LpuRegion_id = msr.LpuRegion_id and msr.Lpu_id = msr2.Lpu_id
							
						) as MedStaffFact_List,
						msf.LpuUnit_id,
						ls.LpuSectionProfile_id
					from v_MedStaffRegion msr
					inner join v_MedStaffFact msf on msf.MedStaffFact_id = msr.MedStaffFact_id and 
				        cast(coalesce(msr.MedStaffRegion_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date) and
				        cast(coalesce(msf.WorkData_endDate, '2030-01-01') as date) >= cast((select curdate from cte) as date)
					inner join v_LpuSection ls on ls.LpuSection_id = msf.LpuSection_id
					 	inner join v_MedSpecOms mso on mso.MedSpecOms_id = msf.MedSpecOms_id and coalesce(mso.MedSpecOms_IsPortalPost, 2) = 2 and mso.MedSpecOms_PortalName is not null 
					where
						msf.Lpu_id = lr.Lpu_id
						and lr.LpuRegion_id = msr.LpuRegion_id
				) msf on true
				left join v_LpuUnit lu on lu.LpuUnit_id = msf.LpuUnit_id
				left join v_Lpu l on lu.Lpu_id = l.Lpu_id
				left join v_DataStorage dshi on l.Lpu_id = dshi.Lpu_id and dshi.DataStorage_Name = 'homevizit_isallowed'
				left join v_DataStorage dshnp on l.Lpu_id = dshnp.Lpu_id and dshnp.DataStorage_Name = 'homevizit_nmp_phone'
				left join v_DataStorage dshp on l.Lpu_id = dshp.Lpu_id and dshp.DataStorage_Name = 'homevizit_phone'
				left join v_DataStorage dshsp on l.Lpu_id = dshsp.Lpu_id and dshsp.DataStorage_Name = 'homevizit_smp_phone'
				left join LpuSectionProfile lsp on msf.LpuSectionProfile_id = lsp.LpuSectionProfile_id
			where lu.LpuUnitType_id = 2
                and l.Lpu_id = lr.Lpu_id
				and COALESCE(l.Lpu_IsTest, 1) = 1
				and (l.Lpu_endDate is null or l.Lpu_endDate >= (select curdate from cte))
				
             and lrs.KLStreet_id is null and lrs.LpuRegionStreet_IsAll = 2 and lrs.KLTown_id = '347215' 
            order by "Lpu_id", "LpuRegion_Name"
        
