Link to home
Start Free TrialLog in
Avatar of detownsend
detownsend

asked on

SQL Query - multiple AND/OR

Good afternoon,

I have a SQL query that is not returning the desired results and was hoping someone could help.  We have a query designed to show a current list of patients in the hospital(s) today, ideally also showing patients that were in the hospital today but were discharged today.

I’m querying database SPLC and joining several tables.  The join components work fine.  However, there are 2 bits I’m trying to isolate on that don’t work.  When a patient is discharged, the Round.dischargeDate field changes from its beginning state of NULL to the date & time the patient is discharged.  

Example A:
SELECT Patient.lastName AS 'Patient Name', Patient.firstName AS ' ',Facility.description AS 'Hospital', Round.roomNumber AS 'Room', Provider.lastName AS Rounding, ReferringProvider.lastName AS 'Referring', Patient.problem1 AS 'Problem1', Patient.problem2 AS 'Problem2', Patient.problem3 AS 'Problem3', Patient.notes AS 'Notes'FROM   SPLC.dbo.Round Round INNER JOIN SPLC.dbo.Facility Facility ON Round.facilityID=Facility.facilityID INNER JOIN SPLC.dbo.Patient Patient ON Round.patientID=Patient.patientID INNER JOIN SPLC.dbo.Provider Provider ON Round.providerID=Provider.providerID LEFT OUTER JOIN SPLC.dbo.ReferringProvider ReferringProvider ON Round.referringProviderID=ReferringProvider.referringProviderID WHERE Facility.Description='Hospital-A' AND Round.dischargeDate IS  NULL  OR Round.dischargeDate=GetDate() ORDER BY Round.roomNumber, Provider.lastName

Results: Example_A partly works.  It returns results for the current patients in Hospital_A, but leaves out patients discharged today, i.e. the Round.dischargeDate=GetDate() bit doesn’t seem to be working.

Example B: (This is similar to Example_A, but I’m trying to query several hospitals to show which patients are there today)
SELECT Patient.lastName AS 'Patient Name', Patient.firstName AS ' ',Facility.description AS 'Hospital', Round.roomNumber AS 'Room', Provider.lastName AS Rounding, ReferringProvider.lastName AS 'Referring', Patient.problem1 AS 'Problem1', Patient.problem2 AS 'Problem2', Patient.problem3 AS 'Problem3', Patient.notes AS 'Notes' FROM   SPLC.dbo.Round Round INNER JOIN SPLC.dbo.Facility Facility ON Round.facilityID=Facility.facilityID INNER JOIN SPLC.dbo.Patient Patient ON Round.patientID=Patient.patientID INNER JOIN SPLC.dbo.Provider Provider ON Round.providerID=Provider.providerID LEFT OUTER JOIN SPLC.dbo.ReferringProvider ReferringProvider ON Round.referringProviderID=ReferringProvider.referringProviderID WHERE Facility.Description='Hospital_A’ OR Facility.Description='Hospital_B' OR Facility.Description='Hospital_C' AND Round.dischargeDate IS NULL OR Round.dischargeDate=GetDate() ORDER BY Facility.description, Round.roomNumber, Provider.lastName

Results: this one doesn’t work very well at all.  The results are showing patients from the hospitals listed in the query which is good, but it seems to ignore the Round.dischargeDate portion completely; patients discharged days/weeks ago are listed in the results.

I’ve tried breaking this into 2 parts, using a UNION statement, with the query for hospital name in the top and the discharge criteria in the bottom, but the results were similar; showing all patients regardless of they’d been discharged or not.

Please let me know if I can provide additional information.

Any ideas would be most appreciated.

THANK YOU
ASKER CERTIFIED SOLUTION
Avatar of Bernie Bsen
Bernie Bsen
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of detownsend
detownsend

ASKER

OK, that was just plain stupid of me.  Thanks for your help!