Solved

Need results returned based on prior records

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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