SQLTeam.com | Weblogs | Forums

Unrelated table in query


#1

I am trying to execute the following query. Although it executes correctly I do not get any results.

SELECT     PatientProfile.Birthdate, DoctorFacility.OrgName, ApptType.Name, ApptSlot.Start, ApptSlot.Stop
FROM         PatientProfile INNER JOIN
                      DoctorFacility ON PatientProfile.DoctorId = DoctorFacility.DoctorFacilityId 
                      AND PatientProfile.FacilityId = DoctorFacility.DoctorFacilityId 
                      AND PatientProfile.PrimaryCareDoctorId = DoctorFacility.DoctorFacilityId 
                      AND PatientProfile.RefDoctorId = DoctorFacility.DoctorFacilityId 
                      inner JOIN
                      ApptSlot ON DoctorFacility.DoctorFacilityId = ApptSlot.FacilityId 
                      CROSS JOIN
                      ApptType 
WHERE     (DoctorFacility.OrgName LIKE 'Salem%') AND (ApptType.Name LIKE 'SBX%') OR
                      (ApptType.Name LIKE 'OBOT%')

I am very new to SQL queries so if I am going about this the wrong way please let me know.


#2

break it down. first run:

select count(*) from PatientProfile

then:

    select count(*) FROM         PatientProfile INNER JOIN
                          DoctorFacility ON PatientProfile.DoctorId = DoctorFacility.DoctorFacilityId 
                          AND PatientProfile.FacilityId = DoctorFacility.DoctorFacilityId 
                          AND PatientProfile.PrimaryCareDoctorId = DoctorFacility.DoctorFacilityId 
                          AND PatientProfile.RefDoctorId = DoctorFacility.DoctorFacilityId 
   where (DoctorFacility.OrgName LIKE 'Salem%')

etc. See where the count drops to zero . then focus on that section to understand why all rows are being eliminated