我正在構建一個查詢,我需要從MEMBERS表中檢索所有記錄,其中連接日期(datejoin)在兩個日期之間,某個成員資格類型(mtypeid)之一和從一個特定的位置(siteid)。需要SQL邏輯可能子查詢需要
然後,我需要從SCSCHEDULER(此表存儲所有約會)表中撤回所有預定過去某個日期的成員的約會,具有某種類型的約會,並且仍然是活性。
我有以下,但我相信我需要在子查詢中放置一些這種邏輯,因爲我只爲具有約會的成員返回數據。我需要至少爲沒有任何約會的成員以及那些有約會的成員和scscheduler數據從成員表中返回數據。
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')
注意@Merlin,你可以通過使用IN()來縮短你的'WHERE MEMEBER.mtypeid = ...或...'。打字少了很多。 https://msdn.microsoft.com/en-us/library/ms177682.aspx – scsimon