Solved

Need results returned based on prior records

Posted on 2009-07-08
5
164 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do ASP.NET and MVC work together? 4 27
Need to pass a connection through class/Method 1 39
Syntax error 9 45
VB.NET Repostiory Pattern 7 12
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

20 Experts available now in Live!

Get 1:1 Help Now