2016-07-05 59 views
1

我正在構建一個查詢,我需要從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') 
+0

注意@Merlin,你可以通過使用IN()來縮短你的'WHERE MEMEBER.mtypeid = ...或...'。打字少了很多。 https://msdn.microsoft.com/en-us/library/ms177682.aspx – scsimon

回答

1

當您嘗試WHERE子句中篩選對SCSCHEDULES(等後外連接),你最終會下降,你想保持行。他們需要被放置在連接條件或包裹起來派生表如下內:

SELECT m.scancode, m.lname, m.fname, mt.description, m.status, m.datejoin, 
     e.lname AS Expr1, e.fname AS Expr2, 
     s.scheduledatefrom, s.timeduration, s.scheduledescription, s.schedulestatus 
FROM MEMBERS AS m 
     LEFT OUTER JOIN 
     (
     SELECT * FROM SCSCHEDULES 
     WHERE 
       scheduledatefrom >= @rvStartDate 
      AND scheduledescription IN ('First', 'Second') 
      AND schedulestatus = '1' 
     ) AS s 
      ON m.memid = s.memid 
     INNER JOIN MEMBERTYPES AS mt 
      ON m.mtypeid = mt.mtypeid 
     INNER JOIN EMPLOYEES AS e 
      ON m.employeeid = e.employeeid 
WHERE  m.datejoin BETWEEN @rvStartDate AND @rvEndDate 
     AND m.siteid = '779' 
     AND m.mtypeid IN (
      '1', '10', '12', '28', '32', '33', '34', '35', '36', '40, '48' 
     ); 
1

LEFT JOIN,環境對所有的,但第一個表應ON子句中去,而不是WHERE條款:

SELECT m.scancode, m.lname, m.fname, mt.description, m.status, m.datejoin, 
     e.lname AS Expr1, e.fname AS Expr2, 
     s.scheduledatefrom, s.timeduration, s.scheduledescription, s.schedulestatus 
FROM MEMBERS m LEFT OUTER JOIN 
    SCSCHEDULES s 
    ON MEMBERS.memid = s.memid AND scheduledatefrom >= @rvStartDate AND 
     scheduledescription IN ('First', 'Second') AND 
     schedulestatus = '1' INNER JOIN 
    MEMBERTYPES mt 
    ON m.mtypeid = mt.mtypeid INNER JOIN 
    EMPLOYEES e 
    ON m.employeeid = e.employeeid 
WHERE m.datejoin BETWEEN @rvStartDate AND @rvEndDate AND 
     m.siteid = '779' AND 
     m.mtypeid IN ('1', '10', '12', '28', '32', '33', '34', '35', '36', '40, '48'); 

注意:如果您的ID是數字,那麼您應該刪除單引號。混合類型可能會混淆優化器。

另請注意,表別名使查詢更易於編寫和讀取。