Solved

Need results returned based on prior records

Posted on 2009-07-08
5
161 Views
Last Modified: 2013-11-26
I have a query that returns a list of patients including a discharge date, admit date, account number and drg.  It currently lists all patients.  The unique identifier to the patient is the unit number.  The account number is different every time they come in.  I need to know for each patient - if the admitdate is within 30 days of the last disdate and only if the drg was the same for both account numbers.  The query below is very basic - only showing the fields and their tables. I have no idea how to do this....  I've attached some sample data and shaded in the ones that would need to be returned from the query.  
Basically we are trying to find out which patients came back within 30 days for the same thing.

I am writing this report in Visual Studio with SQL Server 2005
Thanks!
SELECT  AbsPatDischarges.DisDate, AbsPat.UnitNumber, AbsPat.AccountNumber, AbsPatDrgData.Drg, AbsPat.PtStatus, AdmPatientFile.AdmitDate

FROM     AbsPat INNER JOIN

               AbsPatDischarges ON AbsPat.UrnABSPAT = AbsPatDischarges.UrnABSPAT INNER JOIN

               AbsPatDrgData ON AbsPatDischarges.UrnABSPAT = AbsPatDrgData.UrnABSPAT INNER JOIN

               AdmPatientFile ON AdmPatientFile.UrnADMPAT = AbsPat.AdmUrn

WHERE  (AbsPat.PtStatus LIKE 'in')

ORDER BY AbsPat.UnitNumber, AbsPatDischarges.DisDate

Open in new window

30DAY-SAMPLE-DATA.xls
0
Comment
Question by:palmer9
  • 4
5 Comments
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24811847
Might be a group by type problem where you need to check where count(*) is > 1 as a subquery - or maybe an inline query where date is within 30 days...

So, need to be looking at rows where account numbers are same, drg are same, and dates within that 30 day requirement.

Hmmmm....

Maybe as simple as


SELECT  AbsPatDischarges.DisDate, AbsPat.UnitNumber, AbsPat.AccountNumber, AbsPatDrgData.Drg, AbsPat.PtStatus, AdmPatientFile.AdmitDate
FROM     AbsPat INNER JOIN
               AbsPatDischarges ON AbsPat.UrnABSPAT = AbsPatDischarges.UrnABSPAT INNER JOIN
               AbsPatDrgData ON AbsPatDischarges.UrnABSPAT = AbsPatDrgData.UrnABSPAT INNER JOIN
               AdmPatientFile ON AdmPatientFile.UrnADMPAT = AbsPat.AdmUrn
WHERE  (AbsPat.PtStatus LIKE 'in')
AND EXISTS ( select NULL from FROM     AbsPat  AP
               INNER JOIN AbsPatDischarges APD ON AP.UrnABSPAT = APD.UrnABSPAT
               INNER JOIN AbsPatDrgData APDD ON APD.UrnABSPAT = APDD.UrnABSPAT
               INNER JOIN AdmPatientFile APF ON APF.UrnADMPAT = AP.AdmUrn
               WHERE ApsPat.AccountNumber = AP.AccountNumber
               and AbsPatDrgData.Drg = APDD.Drg
               and AdmPatientFile.AdmitDate < APD.DisDate + 30)

ORDER BY AbsPat.UnitNumber, AbsPatDischarges.DisDate



0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24811962
Instead of :

 and AdmPatientFile.AdmitDate < APD.DisDate + 30)

Use :

 and AdmPatientFile.AdmitDate between APD.DisDate and APD.DisDate + 30)
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 24811990
One more time...



SELECT AbsPatDischarges.DisDate, AbsPat.UnitNumber, AbsPat.AccountNumber, AbsPatDrgData.Drg, AbsPat.PtStatus, AdmPatientFile.AdmitDate
FROM AbsPat INNER JOIN
AbsPatDischarges ON AbsPat.UrnABSPAT = AbsPatDischarges.UrnABSPAT INNER JOIN
AbsPatDrgData ON AbsPatDischarges.UrnABSPAT = AbsPatDrgData.UrnABSPAT INNER JOIN
AdmPatientFile ON AdmPatientFile.UrnADMPAT = AbsPat.AdmUrn
WHERE (AbsPat.PtStatus LIKE 'in')
AND EXISTS ( select NULL from FROM AbsPat AP
                          INNER JOIN AbsPatDischarges APD ON AP.UrnABSPAT = APD.UrnABSPAT
                          INNER JOIN AbsPatDrgData APDD ON APD.UrnABSPAT = APDD.UrnABSPAT
                          INNER JOIN AdmPatientFile APF ON APF.UrnADMPAT = AP.AdmUrn
                          WHERE ApsPat.UnitNumber = AP.UnitNumber
                          and AbsPatDrgData.Drg = APDD.Drg
                          and AbsPatDischarges.DisDate <> APD.disdate
                          and ApsPat.AccountNumber <> AP.AccountNumber -- Think this differentiates the two visits
                          and AdmPatientFile.AdmitDate between APD.DisDate and APD.DisDate + 30)

ORDER BY AbsPat.UnitNumber, AbsPatDischarges.DisDate

0
 

Author Closing Comment

by:palmer9
ID: 31601295
Perfect - Thank you so much!!!
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24814459
A pleasure - happy to help :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now