多个连接左连接访问/子查询

时间:2016-08-22 14:10:34

标签: sql left-join access

以下代码在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];

1 个答案:

答案 0 :(得分:0)

您的问题不明确,但 MS Access + 其他LEFT JOIN 让我觉得您的主要问题是您在查询中进行了额外的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和右括号之后的左括号