I am building a query where I need to retrieve all of the records from the MEMBERS table where the join date (datejoin) is between two dates, one of a certain membership type (mtypeid), and from a particular location (siteid).
THEN, I need to pull back all of the rows from the SCSCHEDULER (this table stores all appointments) table where the appointments for these members that are scheduled past a certain date, are of a certain type of appointment and are still active.
I have the following but I believe I need to place some of this logic in a subquery because I am only returning data for members that have appointments. I need to return the data from the Member table at the very least for members that do not have any appointments and the member and scscheduler data for those that do have appointments.
SELECT MEMBERS.scancode, MEMBERS.lname, MEMBERS.fname, MEMBERTYPES.description, MEMBERS.status, MEMBERS.datejoin, EMPLOYEES.lname AS Expr1,
EMPLOYEES.fname AS Expr2, SCSCHEDULES.scheduledatefrom, SCSCHEDULES.timeduration, SCSCHEDULES.scheduledescription,
SCSCHEDULES.schedulestatus
FROM MEMBERS LEFT OUTER JOIN
SCSCHEDULES ON MEMBERS.memid = SCSCHEDULES.memid INNER JOIN
MEMBERTYPES ON MEMBERS.mtypeid = MEMBERTYPES.mtypeid INNER JOIN
EMPLOYEES ON MEMBERS.employeeid = EMPLOYEES.employeeid
WHERE (MEMBERS.datejoin BETWEEN @rvStartDate AND @rvEndDate) AND (MEMBERS.siteid = '779') AND (MEMBERS.mtypeid = '1' OR
MEMBERS.mtypeid = '10' OR
MEMBERS.mtypeid = '12' OR
MEMBERS.mtypeid = '28' OR
MEMBERS.mtypeid = '32' OR
MEMBERS.mtypeid = '33' OR
MEMBERS.mtypeid = '34' OR
MEMBERS.mtypeid = '35' OR
MEMBERS.mtypeid = '36' OR
MEMBERS.mtypeid = '40' OR
MEMBERS.mtypeid = '48') AND (SCSCHEDULES.scheduledatefrom >= @rvStartDate) AND (SCSCHEDULES.scheduledescription = 'First' OR
SCSCHEDULES.scheduledescription = 'Second') AND (SCSCHEDULES.schedulestatus = '1')
WHERE MEMEBER.mtypeid = ...or...by usingIN(). It's a lot less typing. msdn.microsoft.com/en-us/library/ms177682.aspx