2016-08-22 108 views
0

以下代碼在MS Access中正常工作。但是,我試圖添加更多的左連接。我的左連接將加入查詢而不是表格。多個連接左連接訪問/子查詢

希望這是有道理的。

欣賞的幫助

SELECT 
    a.[business unit], 
    b.[Total Colleagues in DMS], 
    Sum(IIf([a].[Session Date] Is Not Null,1,0)) AS [Total Bookings], 
    Sum([a.Has Booking been cancelled (Yes/No)]) AS [Total Cancellations], 
    (Sum(IIf([a].[Session Date] Is Not Null,1,0))-Sum([a.Has Booking been cancelled (Yes/No)])) AS [Net Bookings], 
    Sum([a.lookup_attended]) AS Attended, 
    (b.[Total Colleagues in DMS]-(Sum(IIf([a].[Session Date] Is Not Null,1,0))-Sum([a.Has Booking been cancelled (Yes/No)]))) AS [Unbooked Spaces] 
FROM 
    everything AS a 
LEFT JOIN 
(
    SELECT 
     a.[business unit], count(*) AS [Total Colleagues in DMS] 
    FROM 
    (
     SELECT DISTINCT 
      a.lookup_id, 
      A.[BUSINESS UNIT] 
     FROM 
      EVERYTHING AS a 
     WHERE 
      (a.status<>"leaver" Or a.status<>"Not in scope") 
      And 
      A.PROJECT Is Null 
    ) AS [%$##@_Alias] 
    GROUP BY 
     a.[business unit] 
) AS b 
ON 
    a.[business unit] = b.[business unit] 
WHERE 
(
    ((a.status)<>"leaver" Or (a.status)<>"not in scope")  
GROUP BY 
    a.[business unit], 
    b.[Total Colleagues in DMS], 
    b.[business unit]; 
+0

需要後在提問時提供更多信息,例如:查詢的名稱是什麼試圖加入?它與列出的現有表格有何關係?一旦你添加了這個查詢,那麼這個連接和其他表之間的關係可能會被關閉。 – xQbert

+0

有什麼不適合呢?重新格式化你的SQL之後,你的'WHERE'語句中缺少''''。你只是遇到語法錯誤?你能發佈確切的錯誤信息,如果有的話? –

+0

表的確可以是查詢(派生表,VIEW,存儲查詢對象)。要加入第三個表格,您需要將前兩個表格放在parens中,例如'SELECT * FROM(T1加入T2在T1.id = T2.id)JOIN T3 ON T1.id = T3.id'(注意優化器不需要遵守指定的順序)。 – onedaywhen

回答

0

你的問題並不清楚,但的MS Access + 額外LEFT JOIN讓我覺得,你的主要問題是,你所做的額外留在查詢中加入該您沒有在上面顯示過,而且如果您有多個JOIN,則沒有在MS ACCESS SQL中使用括號括住您的表。這給了你一個錯誤。

如果我明顯錯了,你應該更好地解釋你的問題是什麼。

所以這將工作:

SELECT 
    a.[business unit], 
    b.[Total Colleagues in DMS], 
    Sum(IIf([a].[Session Date] Is Not Null,1,0)) AS [Total Bookings], 
    Sum([a.Has Booking been cancelled (Yes/No)]) AS [Total Cancellations], 
    (Sum(IIf([a].[Session Date] Is Not Null,1,0))-Sum([a.Has Booking been cancelled (Yes/No)])) AS [Net Bookings], 
    Sum([a.lookup_attended]) AS Attended, 
    (b.[Total Colleagues in DMS]-(Sum(IIf([a].[Session Date] Is Not Null,1,0))-Sum([a.Has Booking been cancelled (Yes/No)]))) AS [Unbooked Spaces] 
FROM 
    everything AS a 
LEFT JOIN 
(
    SELECT 
     a.[business unit], count(*) AS [Total Colleagues in DMS] 
    FROM 
    (
     SELECT DISTINCT 
      a.lookup_id, 
      A.[BUSINESS UNIT] 
     FROM 
      EVERYTHING AS a 
     WHERE 
      (a.status<>"leaver" Or a.status<>"Not in scope") 
      And 
      A.PROJECT Is Null 
    ) AS [%$##@_Alias] 
    GROUP BY 
     a.[business unit] 
) AS b 
ON 
    a.[business unit] = b.[business unit] 
LEFT JOIN your_query AS q ON q.whatever=a.whatever  

WHERE 
(
    ((a.status)<>"leaver" Or (a.status)<>"not in scope")  
GROUP BY 
    a.[business unit], 
    b.[Total Colleagues in DMS], 
    b.[business unit]; 

但是這將工作:

SELECT 
    a.[business unit], 
    b.[Total Colleagues in DMS], 
    Sum(IIf([a].[Session Date] Is Not Null,1,0)) AS [Total Bookings], 
    Sum([a.Has Booking been cancelled (Yes/No)]) AS [Total Cancellations], 
    (Sum(IIf([a].[Session Date] Is Not Null,1,0))-Sum([a.Has Booking been cancelled (Yes/No)])) AS [Net Bookings], 
    Sum([a.lookup_attended]) AS Attended, 
    (b.[Total Colleagues in DMS]-(Sum(IIf([a].[Session Date] Is Not Null,1,0))-Sum([a.Has Booking been cancelled (Yes/No)]))) AS [Unbooked Spaces] 
FROM 
    ((everything AS a 
LEFT JOIN 
(
    SELECT 
     a.[business unit], count(*) AS [Total Colleagues in DMS] 
    FROM 
    (
     SELECT DISTINCT 
      a.lookup_id, 
      A.[BUSINESS UNIT] 
     FROM 
      EVERYTHING AS a 
     WHERE 
      (a.status<>"leaver" Or a.status<>"Not in scope") 
      And 
      A.PROJECT Is Null 
    ) AS [%$##@_Alias] 
    GROUP BY 
     a.[business unit] 
) AS b 
ON 
    a.[business unit] = b.[business unit]) 
LEFT JOIN your_query AS q ON q.whatever=a.whatever) 

WHERE 
(
    ((a.status)<>"leaver" Or (a.status)<>"not in scope")  
GROUP BY 
    a.[business unit], 
    b.[Total Colleagues in DMS], 
    b.[business unit]; 

通知的FROM和右括號後的左括號每個ON條款