
		
		select 
			vps.Person_id,
			vps.Person_FirNameR as name,
			vps.Person_SurNameR surname,
			vps.Person_SecNameR patronymic,
			vpsnab.PersonSurNameAtBirth_SurName firstsurname,
			vps.Person_BirthDay birthdate,
			vps.Sex_id sex,
			COALESCE(vns.KLCountry_id, 643) citizenship, -- Если не определили KLCountry_id, по умолчанию берем "Россия"
			vps.Person_Snils snils,
			COALESCE(vps.Polis_Num, vps.Person_EdNum) policy_sn,
			CASE WHEN ndt.DocumentType_Code is not null THEN vps.Document_id ELSE NULL END AS document_id,
			CASE WHEN ndt.DocumentType_Code is not null THEN ndt.DocumentType_Code ELSE NULL END AS doc_type,
			CASE WHEN ndt.DocumentType_Code is not null THEN nullif(rtrim(ltrim(vps.Document_Ser)), '') ELSE NULL END as serial,
			CASE WHEN ndt.DocumentType_Code is not null THEN nullif(rtrim(ltrim(vps.Document_Num)), '') ELSE NULL END as num,
			CASE WHEN ndt.DocumentType_Code is not null THEN doc.Document_begDate ELSE NULL END AS date_issue,
			CASE WHEN ndt.DocumentType_Code is not null THEN doc.Document_endDate ELSE NULL END AS date_expiry,
			CASE WHEN ndt.DocumentType_Code is not null THEN COALESCE(doc.OrgDep_Text, vo.Org_Nick) ELSE NULL END AS authority,
			CASE WHEN COALESCE(vns.KLCountry_id, 643) = 643 or doctype.DocumentType_id not in (9, 10, 11, 12, 19, 20, 21, 22, 23, 36, 37) THEN 0 ELSE 1 END docConfirm,  
			z.address_type_code,
			z.region address_region,
			z.fias_address,
			z.area,
			z.prefix_area,
			z.street,
			z.prefix_street,
			z.house_num,
			z.struc_num,
			z.room_num,
			z.live_start_date,
			vpp.PersonPrivilege_id,
			vpp.PrivilegeTypeFRLLO_code benefit_code,
			case when vpt.WhsDocumentCostItemType_id = 1 then vpp.PrivilegeType_Code else null end ext_benefit_code,
			vd.Diag_SCode diagnosis,
			vpp.PersonPrivilege_begDate receive_date,
			vpp.PersonPrivilege_endDate cancel_date
		from personList vpp
		inner join dbo.v_PersonState vps   on vpp.Person_id = vps.Person_id
		inner join v_PrivilegeType vpt   on vpt.PrivilegeType_id = vpp.PrivilegeType_id and COALESCE(vpt.PrivilegeType_isNotUnloadFRLLO,1) = 1
		left join v_nationalitystatus vns   on vps.nationalitystatus_id = vns.nationalitystatus_id
		left join v_Diag vd   on vd.Diag_id = vpp.Diag_id
		left join dbo.v_PersonSurNameAtBirth vpsnab   on vpsnab.Person_id = vps.Person_id
		LEFT JOIN dbo.v_Document doc    ON doc.Document_id = vps.Document_id
		LEFT JOIN dbo.v_DocumentType doctype   ON doctype.DocumentType_id = doc.DocumentType_id
		LEFT JOIN nsi.DocumentType ndt   ON ndt.DocumentType_id = doctype.Frmr_id   
		LEFT JOIN dbo.v_OrgDep vod   on vod.OrgDep_id = doc.OrgDep_id 
		LEFT JOIN dbo.v_org vo   on vo.Org_id = vod.Org_id
		LEFT JOIN dbo.FRLLOSendStatus ss   ON ss.FRLLOExportType_id = 1 and ss.FRLLOSendStatus_DBTableID = vpp.Person_id and ss.FRLLOSendStatus_updDT >= vpp.PersonPrivilege_updDT
		 left join lateral  (
			select z.* 
			from (
				select  
					case when vps.UAddress_id = Address.Address_id then 1 else 3 end address_type_code,
					case 
						when a0.KLAdr_Ocatd is not null then substring(a0.KLAdr_Ocatd, 1, 5)
						when a1.KLAdr_Ocatd is not null then substring(a1.KLAdr_Ocatd, 1, 5)
						when a2.KLAdr_Ocatd is not null then substring(a2.KLAdr_Ocatd, 1, 5)
					end region,
					lower(cast(st.KLStreet_AOGUID as varchar(40))) fias_address,
					case when a1.KLArea_id is not null then a1.KLArea_Name else a2.KLArea_Name end area,
					case when a1.KLArea_id is not null then socr1.KLSocr_Nick else socr2.KLSocr_Nick end prefix_area,
					st.KLStreet_Name street,
					socr3.KLSocr_Nick prefix_street,
					address.Address_House house_num,
					address.Address_Corpus struc_num,
					address.Address_Flat room_num,
					coalesce(
						case 
							when Address.Address_id = vps.UAddress_id then 
							(
								select   a.PersonEvn_begDT 
								from dbo.v_PersonUAddress b 
								inner join dbo.v_PersonEvn a on a.PersonEvn_id=b.PersonUAddress_id 
								where b.Address_id=Address.Address_id
								order by a.PersonEvn_insDT desc
								 LIMIT 1 
							)
							when Address.Address_id = vps.PAddress_id then 
							(
								select   a.PersonEvn_begDT 
								from dbo.v_PersonPAddress b 
								inner join dbo.v_PersonEvn a on a.PersonEvn_id=b.PersonPAddress_id 
								where b.Address_id=Address.Address_id
								order by a.PersonEvn_insDT desc
								 LIMIT 1 
							)
						end, 
						Address.Address_begDate, 
						Address.Address_insDT
					) live_start_date
				from v_Address Address  
				left join v_KLArea a0    on address.KLRgn_id = a0.KLArea_id 
				left join v_KLArea a1    on address.KLTown_id = a1.KLArea_id 
				left join v_KLArea a2    on address.KLCity_id = a2.KLArea_id 
				left join v_KLStreet st    on address.KLStreet_id = st.KLStreet_id 
				left join v_KLSocr socr1    on a1.KLSocr_id = socr1.KLSocr_id 
				left join v_KLSocr socr2    on a2.KLSocr_id = socr2.KLSocr_id 
				left join v_KLSocr socr3    on st.KLSocr_id = socr3.KLSocr_id 
				where vps.UAddress_id = Address.Address_id or vps.PAddress_id = Address.Address_id
			) z
			where z.house_num is not null and z.live_start_date is not null
			  and (
				  z.fias_address is not null or (nullif(rtrim(ltrim(z.area)), '') is not null and nullif(rtrim(ltrim(z.street)), '') is not null)
			  )
		) z  on true 
		WHERE vpp.PrivilegeTypeFRLLO_code is not null
	 
			and ss.FRLLOSendStatus_DBTableID is null
