2

我正在一个 Access 2010 数据库中编写一个查询,该数据库调用同一个 Access 2010 数据库中的另外两个查询。结果将是一个包含以下字段的表格报告:

FirstName, LastName, HomePhone, ClientNumber, Level, nextDate, Type 

客户/行列表由 Query1 生成,它还填充 FirstName、LastName、HomePhone 和 Client Number。然后 Query2 填充上面第一个查询中定义的每个客户行的 Level、nextDate 和 Type。

到目前为止,我的摘要查询是:

SELECT Query1.FirstName, Query1.LastName, Query1.HomePhone, Query1.ClientNumber, Query2.[Level], Query2.nextDate, Query2.Type 
FROM Query1 
INNER JOIN Query2 ON Query1.ClientNumber=Query2.ClientNumber;  

但是,当我尝试在数据表视图中查看此摘要查询的结果时,Access 会给我一个对话框,询问 Query2.ClientNumber,这表明代码中的某些内容没有正确引用该变量。

作为参考,这里是 Query1 的代码:

SELECT ActiveCustomers.FirstName, ActiveCustomers.LastName, tblClientAddress.HomePhone, ActiveCustomers.ClientNumber 
FROM 
(
    SELECT Clients.ClientNumber, Clients.FirstName, Clients.LastName   
    (     
        SELECT COUNT(ReferralDate)      FROM IntakeTable      WHERE Clients.ClientNumber = IntakeTable.ClientNumber AND Len(ReferralDate & '') > 0   
    ) AS IntakeCount, 
    (     
        SELECT COUNT(ExitDate)      FROM ExitTable      WHERE Clients.ClientNumber = ExitTable.ClientNumber AND Len(ExitDate & '') > 0   
    ) AS ExitCount 
FROM Clients
)  AS ActiveCustomers  
INNER JOIN tblClientAddress ON ActiveCustomers.ClientNumber=tblClientAddress.ClientNumber
WHERE IntakeCount > ExitCount  AND tblClientAddress.CurrentResidence = True;  

这是 Query2 的代码:

SELECT a.[Level], max(a.dte) AS nextDate, IIf([Type1Date]<[Type2Date],"t1","t2") AS [Type]
FROM (
    select [Level], Type1Date as dte
    FROM CommunicationTable WHERE ClientNumber=1  
    UNION
    select [Level], Type2Date as dte
    FROM CommunicationTable WHERE ClientNumber = 1  
)  AS a
GROUP BY a.[Level], IIf(a.[Type1Date]<a.[Type2Date],"t1","t2");  

有人可以告诉我如何设置它以使其正常运行吗?

4

1 回答 1

2

当您像这样连接和嵌套查询时,它有助于描绘正在“暴露”给外部查询的内容。Query2有一个a不公开的嵌套查询 ,ClientNumber因此Query2不能(也不会尝试)公开ClientNumber到联接。试试这个Query2

SELECT a.[Level], max(a.dte) AS nextDate, IIf([Type1Date]<[Type2Date],"t1","t2") AS [Type], a.ClientNumber
FROM (
    select [Level], Type1Date as dte, ClientNumber
    FROM CommunicationTable WHERE ClientNumber=1  
    UNION
    select [Level], Type2Date as dte
    FROM CommunicationTable WHERE ClientNumber = 1  
)  AS a
GROUP BY a.[Level], IIf(a.[Type1Date]<a.[Type2Date],"t1","t2");

这样,外部查询可以“看到”连接两侧的 ClientNumber 列。

于 2013-10-05T12:02:58.453 回答