I'm hoping that someone can give some guidance on a better way to do this as I'm fairly new to SQL programming. I'm trying to filter out all our clients based on the last meeting date where they were in the office date. If they have been in the past 2 years, I don't want to see their person_id at all. However, in the table being used "Patient_encounter", all the person_ids are associated with the encounter_id (unique identifer) which is written to the file every time a client is checked in. So I'm running into issues due to the one-to-many ratio is in this table, where 1 person_id is usually associated to many encounter_ids. Can someone suggest a better way to do this as the very last select statement is causing issues, and I'm pretty sure the way I'm doing it is very ineffective.declare @LastVisit datetimedeclare @personid varchar (50)declare @Years varchar(2)declare @enc_id varchar(50)-- Create temp table to hold last visit datescreate table #LastVisit ( PersonID varchar(50), LastVisit datetime,enc_id varchar(50),Years varchar(2))--Retrieve the last visit datedeclare contact_cursor CURSOR FORselect distinct person_id, enc_timestamp,enc_id,datediff(yyyy,enc_timestamp,getdate()) from patient_encounterwhere datediff(yyyy,enc_timestamp,getdate()) > 2order by enc_timestamp descOpen contact_cursor-- Perform the first fetch.FETCH NEXT FROM contact_cursor into @PersonID, @LastVisit,@enc_id,@Years;insert into #LastVisit (PersonID,LastVisit,enc_id,Years) values(@PersonID,@LastVisit,@enc_id,@Years)-- Check @@FETCH_STATUS to see if there are any more rows to fetch.WHILE @@FETCH_STATUS = 0BEGIN -- This is executed as long as the previous fetch succeeds. insert into #LastVisit (PersonID,LastVisit,enc_id,Years) values(@PersonID,@LastVisit,@enc_id,@Years) FETCH NEXT FROM contact_cursor into @PersonID, @LastVisit,@enc_id,@Years;ENDCLOSE contact_cursor;DEALLOCATE contact_cursor;GO-------------------------------------------------------------Create a temp table to store the <=2 year appointmentsdeclare @LastCurrentVisit datetimedeclare @CurrentPersonId varchar (50)declare @CurrentYears varchar(2)declare @CurrentEnc_Id varchar(50)-- Create temp table to hold last visit datescreate table #LastCurrentVisit ( CurrentPersonID varchar(50), CurrentLastVisit datetime,CurrentEnc_Id varchar(50),CurrentYears varchar(2))--Retrieve the last visit datedeclare contact_cursor CURSOR FORselect distinct person_id, enc_timestamp,enc_id,datediff(yyyy,enc_timestamp,getdate()) from patient_encounterwhere datediff(yyyy,enc_timestamp,getdate()) <= 2order by enc_timestamp descOpen contact_cursor-- Perform the first fetch.FETCH NEXT FROM contact_cursor into @CurrentPersonID, @LastCurrentVisit,@CurrentEnc_Id,@CurrentYears;insert into #LastCurrentVisit (CurrentPersonID,CurrentLastVisit,CurrentEnc_Id,CurrentYears) values(@CurrentPersonID, @LastCurrentVisit,@CurrentEnc_Id,@CurrentYears)-- Check @@FETCH_STATUS to see if there are any more rows to fetch.WHILE @@FETCH_STATUS = 0BEGIN -- This is executed as long as the previous fetch succeeds. insert into #LastCurrentVisit (CurrentPersonID,CurrentLastVisit,CurrentEnc_Id,CurrentYears) values(@CurrentPersonID, @LastCurrentVisit,@CurrentEnc_Id,@CurrentYears) FETCH NEXT FROM contact_cursor into @CurrentPersonID, @LastCurrentVisit,@CurrentEnc_Id,@CurrentYears;ENDCLOSE contact_cursor;DEALLOCATE contact_cursor;GOselect p.first_name, p.last_name, p.address_line_1,p.city,p.state,p.zip,p.home_phone, p.date_of_birthfrom person p, #LastCurrentVisit, #LastVisit--This where clause is causing issueswhere #LastVisit.PersonID != #LastCurrentVisit.CurrentPersonIDand p.expired_ind = 'N'order by p.last_name asc