create temp table personList on commit drop as
			 
		select *
		  from (
		select distinct
			vpp.person_id, vpp.PersonPrivilege_id, vpp.PrivilegeType_id,
			vpp.PrivilegeType_Code, ptf.PrivilegeTypeFRLLO_code, vpp.Diag_id,
			vpp.PersonPrivilege_begDate, vpp.PersonPrivilege_endDate,
			MAX(vpp.PersonPrivilege_updDT) over (partition by vpp.person_id) PersonPrivilege_updDT
		from
			v_PersonPrivilege vpp  
			inner join nsi.v_PrivilegeTypeFRLLOLink ptfl   on ptfl.PrivilegeType_id = vpp.PrivilegeType_id
			inner join nsi.v_PrivilegeTypeFRLLO ptf   on ptf.PrivilegeTypeFRLLO_id = ptfl.PrivilegeTypeFRLLO_id
			left join dbo.v_PersonCard pc   on pc.Person_id = vpp.Person_id and pc.LpuAttachType_id = 1 and GETDATE() between pc.PersonCard_begDate and COALESCE (pc.PersonCard_endDate, GETDATE() )
			left join dbo.v_Lpu l   on l.Lpu_id = pc.Lpu_id
			left join dbo.v_OrgNotExportFRLLO ns   on l.Org_id = ns.Org_id
			left join (
				select l.Person_id, max(l.FRLLOExportJournalLog_insDT) FRLLOExportJournalLog_insDT
				from dbo.FRLLOExportJournalLog l  
				inner join dbo.FRLLOExportJournal fj  
				on l.FRLLOExportJournal_id = fj.FRLLOExportJournal_id and FRLLOExportStatus_id = 4
				where l.FRLLOExportType_id = 1 --and fjl.FRLLOPersonPrivilege_RegNum is not null
				group by l.Person_id
			)
			fjl on fjl.Person_id = vpp.Person_id
		where
		COALESCE(vpp.PersonPrivilege_endDate, cast('2020-12-01' as date)) >= cast('2020-12-01' as date) and ns.Org_id
		is null and
		 
				(
					fjl.FRLLOExportJournalLog_insDT is null -- новые льготы
					or vpp.PersonPrivilege_updDT >= fjl.FRLLOExportJournalLog_insDT --были изменения в самой льготе
				)
			 ) vpp where
		 vpp.PersonPrivilege_updDT::date >= $1::date 
