Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need results returned based on prior records

Posted on 2009-07-08
5
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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