Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Filtering Clients based on last meeting date

Author  Topic 

cant2ny
Starting Member

20 Posts

Posted - 2011-04-04 : 15:40:50
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 datetime
declare @personid varchar (50)
declare @Years varchar(2)
declare @enc_id varchar(50)

-- Create temp table to hold last visit dates
create table #LastVisit (
PersonID varchar(50), LastVisit datetime,enc_id varchar(50),Years varchar(2))

--Retrieve the last visit date
declare contact_cursor CURSOR FOR
select distinct person_id, enc_timestamp,enc_id,datediff(yyyy,enc_timestamp,getdate()) from patient_encounter
where datediff(yyyy,enc_timestamp,getdate()) > 2
order by enc_timestamp desc

Open 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 = 0
BEGIN

-- 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;
END

CLOSE contact_cursor;
DEALLOCATE contact_cursor;
GO

-----------------------------------------------------------
--Create a temp table to store the <=2 year appointments
declare @LastCurrentVisit datetime
declare @CurrentPersonId varchar (50)
declare @CurrentYears varchar(2)
declare @CurrentEnc_Id varchar(50)

-- Create temp table to hold last visit dates
create table #LastCurrentVisit (
CurrentPersonID varchar(50), CurrentLastVisit datetime,CurrentEnc_Id varchar(50),CurrentYears varchar(2))
--Retrieve the last visit date
declare contact_cursor CURSOR FOR
select distinct person_id, enc_timestamp,enc_id,datediff(yyyy,enc_timestamp,getdate()) from patient_encounter
where datediff(yyyy,enc_timestamp,getdate()) <= 2
order by enc_timestamp desc

Open 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 = 0
BEGIN

-- 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;
END

CLOSE contact_cursor;
DEALLOCATE contact_cursor;
GO

select p.first_name, p.last_name, p.address_line_1,p.city,p.state,p.zip,p.home_phone,
p.date_of_birth
from person p, #LastCurrentVisit, #LastVisit
--This where clause is causing issues
where #LastVisit.PersonID != #LastCurrentVisit.CurrentPersonID
and p.expired_ind = 'N'
order by p.last_name asc

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-04 : 15:52:11
Why not something like this:



select
p.first_name,
p.last_name,
p.address_line_1,
p.city,
p.state,
p.zip,
p.home_phone,
p.date_of_birth
from person p
left Join (
Select PersonId, LastVisit = Max(enc_timestamp)
From patient_encounter
Group By PersonId
) LV
On P.PersonId = LV.PersonId
Where p.expired_ind = 'N'
and (
LV.PersonId is null -- has no visit
or
--LV.LastVisit < dateadd(yy,-2,getdate()) -- last visit is more than 2 years ago
LV.LastVisit < dateadd(yy,-2,dateadd(dy,datediff(dy,0,getdate()),0)) -- last visit is more than 2 years ago (with the time stripped)
)
order by p.last_name asc


Corey

I Has Returned!!
Go to Top of Page

cant2ny
Starting Member

20 Posts

Posted - 2011-04-04 : 16:19:14
Thank you - so much simpler then what I was trying to do. Thanks again.
Go to Top of Page
   

- Advertisement -