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
detownsendAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bernie BsenChef de cuisineCommented:
You need to set Brackets like

...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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
detownsendAuthor Commented:
OK, that was just plain stupid of me.  Thanks for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.