SQLTeam.com | Weblogs | Forums

Need better query results

sql2008r2

#1

I have created the following query.

DECLARE 
 @StartDate Datetime = '2014-11-01'
 , @EndDate DateTime = '2014-12-01'

SELECT     Appointments.ApptStart, Appointments.Status, DoctorFacility.Last, DoctorFacility.First, PatientVisit.PatientVisitId, 
            PatientVisit.PatientProfileId
FROM         DoctorFacility, PatientVisit
                      
                      
           left join Appointments ON
                      Appointments.PatientVisitID=PatientVisit.PatientvisitID
                      
 where     (DoctorFacility.Last = 'Cambell')  OR
                      (DoctorFacility.Last = 'Frost') OR
                      (DoctorFacility.Last = 'Maden') OR
                      (DoctorFacility.Last = 'Morell') OR
                      (DoctorFacility.Last = 'Schott')
                      AND (Appointments.Status like 'Cancel%' )

However, the query is pulling all status from the Appointments.Status column instead of Cancel. I have tried both like as well as equal. Same results. What am I doing wrong?


#2

You need couple of brackets so the OR conditions don't take precedence. See here

where      (
                      (DoctorFacility.Last = 'Cambell')  OR
                      (DoctorFacility.Last = 'Frost') OR
                      (DoctorFacility.Last = 'Maden') OR
                      (DoctorFacility.Last = 'Morell') OR
                      (DoctorFacility.Last = 'Schott')
            )
                      AND (Appointments.Status like 'Cancel%' )

#3

Well that was certainly a quick fix. Thank you very much for your help.