        
			with av as (select
							AV.AttributeValue_TablePKey,
							count(AV.AttributeValue_id) as cc
						from
							v_AttributeValue AV
							inner join v_Attribute A on A.Attribute_id = AV.Attribute_id and A.Attribute_SysNick in ('portalzno', 'EarlyDetect')
							left join v_AttributeVision AVI on AVI.Attribute_id = A.Attribute_id
							left join v_AttributeSignValue ASV on ASV.AttributeSignValue_id = AV.AttributeSignValue_id
						where
							AV.AttributeValue_TableName = 'dbo.LpuSection'
							and AV.AttributeValue_ValueBoolean = '1'
							--and AV.AttributeValue_TablePKey =msf.LpuSection_id -- ls.LpuSection_id
							and cast(COALESCE(AVI.AttributeVision_begDate, '2000-01-01') as date) <= (select dbo.tzgetdate())
							and cast(COALESCE(AVI.AttributeVision_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
							and cast(COALESCE(A.Attribute_begDate, '2000-01-01') as date) <= (select dbo.tzgetdate())
							and cast(COALESCE(A.Attribute_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
							and cast(COALESCE(ASV.AttributeSignValue_begDate, '2000-01-01') as date) <= (select dbo.tzgetdate())
							and cast(COALESCE(ASV.AttributeSignValue_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
							group by AV.AttributeValue_TablePKey
						)
			select distinct
				ls.LpuSection_id,
				(rtrim(msf.Person_surname)||' '||rtrim(msf.Person_firname)||' '||COALESCE(rtrim(msf.Person_secname),'')) as "FullName",
				(rtrim(msf.Person_surname)||' '||rtrim(msf.Person_firname)||' '||COALESCE(rtrim(msf.Person_secname),'')) as "MedPersonal_FullName",
				msf.Person_surname as "SurName",
				rtrim(msf.Person_firname) as "FirName",
				coalesce(rtrim(msf.Person_secname),'') as "SecName",
				msf.MedPersonal_id as "MedPersonal_id",
				lsp.LpuSectionProfile_id as "LpuSectionProfile_id",
				lsp.LpuSectionProfile_mainid as "LpuSectionProfile_mainid",
				lsp.LpuSectionProfile_Name as "LpuSectionProfile_Name",
				lr.LpuRegion_Name as "LpuRegion_Name",
				lr.LpuRegion_id as "LpuRegion_id",
				ls.LpuSection_Name as "LpuSection_Name",
				ps.Sex_id as "PersonSex_id",
				msf.LpuUnit_id as "LpuUnit_id",
				msf.Lpu_id as "Lpu_id",
				msf.MedStaffFact_Descr as "MedStaffFact_Descr",
				msf.MedStaffFact_updDT as "MedStaffFact_updDT",
				msf.MedStaffFact_id as "MedStaffFact_id",
				msf.RecType_id as "RecType_id",
				msf.MedStatus_id as "MedStatus_id",
				lb.LpuBuilding_id as "LpuBuilding_id",
				lb.LpuBuilding_Name as "LpuBuilding_Name",
				COALESCE(lbs.KLStreet_Name || ', ', '') || COALESCE(lba.Address_House, '') || COALESCE(', корп. ' || lba.Address_Corpus, '') as "LpuBuilding_Address",
				rtrim(lsp.ProfileSpec_Name) as "ProfileSpec_Name",
				rtrim(mso.MedSpecOms_PortalName) as "MedSpecOms_PortalName",
				mso.MedSpecOms_id as "MedSpecOms_id",
				RecType_Descr as "RecType_Descr",
				case when lr.LpuRegion_id is not null then 1 end as "LpuRegion_Is",
				case when Terr.ERTerr_id is not null then 1 end as ERTerr_Is,
				rtrim(l.Lpu_Nick) as "Lpu_Nick",
				rtrim(l.Lpu_Name) as "Lpu_Name",
				rtrim(lu.LpuUnit_Name) as "LpuUnit_Name",
				COALESCE(luac.KLArea_Name || ', ', '') || COALESCE(luat.KLArea_Name || ', ', '') || luas.KLStreet_Name || ', ' || lua.Address_House || COALESCE(', корп. ' || lua.Address_Corpus, '') as "LpuUnit_Address",
				c.name as "QualificationCat_Name",
				lu.LpuUnit_Phone as "LpuUnit_Phone",
				eq.IsElectronicQueueEnabled as "IsElectronicQueueEnabled",
				CASE
					WHEN (lb.LpuBuilding_Latitude is not null AND lb.LpuBuilding_Longitude is not null) then lb.LpuBuilding_Latitude || ' ' || lb.LpuBuilding_Longitude --для карты нужны точные координаты зданий
					WHEN klhc.KLHouseCoords_LatLng is not null then klhc.KLHouseCoords_LatLng --и возможно, территорий
				ELSE NULL
				END as "KLHouseCoords_LatLng",
				pd.PersonDisp_id as "PersonDisp_id",
				vc.videochatIsOn as "videochatIsOn",
				msf.MedStafffactCache_CostRec as "MedStafffactCache_CostRec"
			from v_MedStaffFact msf 
				inner join v_Lpu l  on msf.Lpu_id = l.Lpu_id 
				inner join v_LpuUnit lu on lu.LpuUnit_id = msf.LpuUnit_id
				inner join v_LpuSection ls  on msf.LpuSection_id = ls.LpuSection_id
				inner join LpuSectionProfile lsp  on ls.LpuSectionProfile_id = lsp.LpuSectionProfile_id
				left join av on av.AttributeValue_TablePKey= msf.LpuSection_id
				left join persis.WorkPlace wp  on msf.MedStaffFact_id = wp.id
				left join v_PersonDisp pd on (
					msf.MedPersonal_id = pd.MedPersonal_id 
					and pd.Person_id = 0
					and pd.PersonDisp_endDate is null
				)
				left join v_LpuBuilding lb  on lb.LpuBuilding_id = ls.LpuBuilding_id
				left join v_LpuBuildingStreet lbstr  on (lbstr.LpuBuilding_id = lb.LpuBuilding_id and lbstr.LpuBuildingStreet_IsAll = 2)
				left join v_Address lba on lba.Address_id = lb.Address_id
				left join v_KLStreet lbs on lbs.KLStreet_id = lba.KLStreet_id
				left join v_MedStaffRegion msr  on msr.MedStaffFact_id = msf.MedStaffFact_id and cast(coalesce(msr.MedStaffRegion_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
				left join v_PersonState ps  on msf.Person_id = ps.Person_id
				left join MedSpecOms mso on mso.MedSpecOms_id = msf.MedSpecOms_id
				left outer join Address lua  on lu.Address_id = lua.Address_id
				left outer join KLStreet luas  on lua.KLStreet_id = luas.KLStreet_id
				left outer join KLArea luat  on lua.KLTown_id = luat.KLArea_id
				left outer join KLArea luac  on lua.KLCity_id = luac.KLArea_id
				left outer join KLHouseCoords klhc  on lua.KLStreet_id = klhc.KLStreet_id and lua.Address_House = klhc.KLHouseCoords_Name
				left join lateral (
					select
						( select LpuRegion_Name || ', ' as "data()"
							from v_MedStaffRegion t2 
							left join v_LpuRegion lr2 on t2.LpuRegion_id = lr2.LpuRegion_id
							where t1.MedStaffFact_id = t2.MedStaffFact_id
								and cast(coalesce(t2.MedStaffRegion_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
							order by cast(lr2.LpuRegion_Name as int)
							 limit 1
						) as LpuRegion_Name,
						( select cast(LpuRegion_id as varchar) || ', ' as "data()"
							from v_MedStaffRegion t2 
							where t1.MedStaffFact_id = t2.MedStaffFact_id
								and cast(coalesce(t2.MedStaffRegion_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
								limit 1
						) as LpuRegion_id,
						t1.Lpu_id
					from v_MedStaffRegion t1
					left join v_LpuRegion lr1  on t1.LpuRegion_id = lr1.LpuRegion_id
					where t1.MedStaffFact_id = msf.MedStaffFact_id
					and cast(coalesce(t1.MedStaffRegion_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
					
					group by t1.MedStaffFact_id, t1.LpuRegion_id, t1.Lpu_id
				) lr on true 
				
            left join lateral (
				 select
					 case when ttg.TimeTableGraf_id is not null then 1 else 0 end as videochatIsOn
                 from v_TimeTableGraf ttg				
				 where 
                     ttg.MedStaffFact_id = msf.MedStaffFact_id
                     and ttg.TimeTableGraf_id is not null
                     and ttg.TimeTableType_id = 13 -- находим не занятые бирки с типом видеосвязь
                     and ttg.Person_id is null					
                     and ttg.TimeTableGraf_begTime > (select dbo.tzgetdate())
                     limit 1
            ) vc on true
				left join lateral (
					select 
					    CASE
					        WHEN mseq.MedServiceElectronicQueue_id IS NOT NULL THEN 1 ELSE 0
                            END AS IsElectronicQueueEnabled
					from v_MedServiceElectronicQueue mseq 
					    inner join v_ElectronicService es on es.ElectronicService_id = mseq.ElectronicService_id
					    inner join v_ElectronicQueueInfo eqi on eqi.ElectronicQueueInfo_id = es.ElectronicQueueInfo_id
					where (1=1)
					    and mseq.MedStaffFact_id = msf.MedStaffFact_id
					    and eqi.ElectronicQueueInfo_IsOff = 1
					limit 1
				) eq on true
				left join RecType zt on msf.RecType_id = zt.RecType_id
				left join lateral (
					select c.name
					from persis.QualificationCategory qc 
					left outer join persis.Category c on qc.Category_id = c.id
					where msf.MedPersonal_id = qc.MedWorker_id
					order by qc.AssigmentDate desc
					limit 1
				) c on true
				
				inner join ERTerr Terr on
				(
					(
						(lua.KLCountry_id = Terr.KLCountry_id or Terr.KLCountry_id is null) and
						(lua.KLRGN_id = Terr.KLRGN_id or Terr.KLRGN_id is null) and
						(lua.KLSubRGN_id = Terr.KLSubRGN_id or Terr.KLSubRGN_id is null) and
						(lua.KLCity_id = Terr.KLCity_id or Terr.KLCity_id is null) and
						(lua.KLTown_id = Terr.KLTown_id or Terr.KLTown_id is null)
					) or (
						(lbstr.KLSubRGN_id = Terr.KLSubRGN_id or Terr.KLSubRGN_id is null) and
						(lbstr.KLCity_id = Terr.KLCity_id or Terr.KLCity_id is null)
					)
				) and Terr.ERTerr_id = '520101000000026' and lua.KLCountry_id is not null and lua.KLRGN_id is not null
			
			where (1=1)
			    
				
				
				and (COALESCE(LpuSectionAge_id, 3) in (2, 3)) and lu.LpuUnitType_id != 6
				--  and COALESCE(l.Lpu_IsTest, 1) = 1 переписан
     			and (l.Lpu_IsTest = 1 or l.Lpu_IsTest is null)
				and lu.lpuunit_isenabled = 2
				and lu.LpuUnitType_id in (2, 5, 10, 12)
				 and (lsp.LpuSectionProfile_id = '520101000000150' or lsp.LpuSectionProfile_mainid = '520101000000150' )
				
				
				and (cast(coalesce(msf.WorkData_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate()))
				and COALESCE(msf.RecType_id, 6) not in (2,5,6,8)
				and l.Lpu_id is not null
				and cast(coalesce(l.Lpu_endDate, '2030-01-01') as date) >= (select dbo.tzgetdate())
				and msf.Person_surname is not null
				--  and COALESCE(msf.MedStaffFactCache_IsNotShown, 0) != 2
  				and (msf.MedStaffFactCache_IsNotShown != 2 or msf.MedStaffFactCache_IsNotShown is null)
								and( av.cc != 2 or av.cc is null)
			order by
				pd.PersonDisp_id desc,
				case when lr.LpuRegion_id is not null then 1 end desc,
				"LpuUnit_Name",
				msf.Person_surname
		
