Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am able to get the population with admission order from current census and the total census from the queries below respectively. For one time I can get the total population and subtract the population with admission order to get the "population with no admission order" but now i need to schedule this report to run daily and I tried different ways using NOT IN, LEFT JOIN and NOT EXISTS but it is not working. Can any one please suggest me what I am doin wrong here:

Query for patient with admission order:

SQL
SELECT DISTINCT  a.PatientAccountID
,a.VisitStartDateTime


FROM PatientVisitInfo a with (nolock)
INNER JOIN HOrder  b with (nolock)
ON a.PatientVisit_oid=b.PatientVisit_oid


WHERE a.VisitTypeCode='IP'
AND a.VisitStartDateTime >= GETDATE()-1
AND(b.OrderAbbr ='A_AdmitObs'
or b.OrderAbbr ='A_AdmitInpt'
or b.OrderAbbr ='A_CngInpt'
or b.OrderAbbr ='A_CngObs')

ORDER BY  a.PatientAccountID


query for current census:
SQL
SELECT DISTINCT  a.PatientAccountID
,a.VisitStartDateTime


FROM PatientVisitInfo a with (nolock)
INNER JOIN HOrder  b with (nolock)
ON a.PatientVisit_oid=b.PatientVisit_oid

WHERE a.VisitTypeCode='IP'
AND a.VisitStartDateTime >= GETDATE()-1

ORDER BY  a.PatientAccountID


Query I tried for patient with no admission order

SQL
SELECT DISTINCT  a.PatientAccountID
,a.VisitStartDateTime

FROM PatientVisitInfo a with (nolock)
INNER JOIN HOrder  b with (nolock)
ON a.PatientVisit_oid=b.PatientVisit_oid

WHERE a.VisitTypeCode='IP'
AND a.VisitStartDateTime >= GETDATE()-1
AND(b.OrderAbbr <>'A_AdmitObs'
or b.OrderAbbr <>'A_AdmitInpt'
or b.OrderAbbr <>'A_CngInpt'
or b.OrderAbbr <>'A_CngObs')

ORDER BY  a.PatientAccountID

BUT the results of this query will give me the total census.

Thank you for any help!!
Posted
Updated 22-Dec-14 7:15am
v2

1 solution

Hi,

When you said a patient with no admission order, is it a patient without any records in your Horder table? if it is the case, you need to do this,
SQL
SELECT DISTINCT  a.PatientAccountID
,a.VisitStartDateTime

FROM PatientVisitInfo a with (nolock)
LEFT JOIN HOrder  b with (nolock)
ON a.PatientVisit_oid=b.PatientVisit_oid

WHERE a.VisitTypeCode='IP'
AND a.VisitStartDateTime >= GETDATE()-1
AND b.PatientVisit_oid IS NULL

ORDER BY  a.PatientAccountID


Now if you want the patient not having b.OrderAbbr, use a sub query and left join.

SQL
SELECT DISTINCT  a.PatientAccountID
,a.VisitStartDateTime

FROM PatientVisitInfo a with (nolock)
LEFT JOIN (SELECt * FROM HOrder  with (nolock)
WHERE b.OrderAbbr = 'A_AdmitObs'
or b.OrderAbbr = 'A_AdmitInpt'
or b.OrderAbbr = 'A_CngInpt'
or b.OrderAbbr = 'A_CngObs') AS B
ON a.PatientVisit_oid=b.PatientVisit_oid

WHERE a.VisitTypeCode='IP'
AND a.VisitStartDateTime >= GETDATE()-1
AND b.PatientVisit_oid IS NULL

ORDER BY  a.PatientAccountID

Let me know which one could work for you.
 
Share this answer
 
v2
Comments
alicashah 22-Dec-14 13:35pm    
Thank you solution 2 worked.

Thank you once again!!!
Maciej Los 22-Dec-14 13:56pm    
+5 for effort!
Lolo1986 23-Dec-14 14:05pm    
Thanks guys! wish you happy holidays !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900