指定的字段可以引用FROM子句中列出的多个表

时间:2012-08-12 10:38:12

标签: sql ms-access join

我有以下代码。我的问题是我想从另一个表名称Employee中检索每个员工姓名,其中EmployeeID列类似于我在当前查询中使用的AccessLog表。现在,当我加入这两个表时,它给出了我在标题上说的奇怪错误。没有添加的代码 - 第一个SELECT中的A.EmployeeID,Employee.FirstName;作为INNER JOIN Employee ON A.EmployeeID = Employee.EmployeeID到最后一个ORDER By子句,它运行良好。我认为问题在于命名/别名。我看到有类似的线程,但那些并没有解决我的问题。如果有人能指出我的错,那就会很感激。

SELECT **A.EmployeeID,
    Employee.FirstName,**
    MonthName(Month([LogDate])) AS MonthName,
    Round((Sum(Int(DateDiff("s",'00:00:00',Duration)))/3600)) AS TotalTime
    FROM (
         SELECT AccessLog.EmployeeID, 
                AccessLog.LogDate, 
                AccessLog.TerminalID, 
                AccessLog.LogTime, 
                Format((SELECT max(LogTime) 
                        FROM AccessLog AS Alias 
                        WHERE Alias.LogTime < AccessLog.LogTime 
                        AND Alias.EmployeeID = AccessLog.EmployeeID 
                        AND Alias.LogDate = AccessLog.LogDate 
                        AND (Alias.TerminalID)<>"iGuard1A" 
                        And (Alias.TerminalID)<>"iGuard1B"  
                        AND Alias.EmployeeID = AccessLog.EmployeeID),"hh:nn:ss") AS PrevTime, 
                Format((ElapsedTime(iif(PrevTime = '',logtime,prevtime),[LogTime])),"hh:nn:ss") AS Duration, 
                AccessLog.InOut 
             FROM AccessLog 
             WHERE (((AccessLog.TerminalID)<>"iGuard1A" 
             And (AccessLog.TerminalID)<>"iGuard1B") 
             AND ((AccessLog.EmployeeID) Like "2*") 
             AND ((AccessLog.InOut)="OUT")) 
             ORDER BY AccessLog.EmployeeID, AccessLog.LogDate, AccessLog.LogTime)  
             **AS A INNER JOIN Employee ON A.EmployeeID= Employee.EmployeeID**
      GROUP BY EmployeeID, MonthName(Month([LogDate]));

2 个答案:

答案 0 :(得分:1)

我认为可能是GROUP BY EmployeeId - 尝试将其更改为GROUP BY A.EmployeeIdGROUP BY Employee.EmployeeId

编辑:它可能必须是GROUP BY A.EmployeeId,因为那是使用SELECT的那个。

答案 1 :(得分:0)

我解决了。正如我所说,有名称/别名问题。我必须为每个子查询中使用的同一个表分配单独的别名。即使之后它显示顶部选择中的firstname的另一个aggragate函数错误,我也通过将其包含在子查询中来解决这个问题。所以完整我的代码如下:

SELECT C.EmployeeID, 
   (SELECT Employee.FirstName& ' ' &Employee.LastName 
      FROM Employee 
      where C.EmployeeID= Employee.EmployeeID) 
    AS FullName, 
   MonthName(Month([LogDate])) AS MonthName,
   Round((Sum(Int(DateDiff("s",'00:00:00',Duration)))/3600)) AS TotalTime
   FROM (SELECT B.EmployeeID, 
              B.LogDate, 
              B.TerminalID, 
              B.LogTime, 
              Format((SELECT max(LogTime) 
                      FROM AccessLog AS A 
                      WHERE A.LogTime < B.LogTime 
                      AND A.EmployeeID = B.EmployeeID  
                      AND A.LogDate = B.LogDate 
                      AND (A.TerminalID)<>"iGuard1A" 
                      And (A.TerminalID)<>"iGuard1B"  
                      AND A.EmployeeID = B.EmployeeID),"hh:nn:ss") AS PrevTime,  
              Format((ElapsedTime(iif(PrevTime = '',logtime,prevtime),[LogTime])),"hh:nn:ss") AS Duration, 
              B.InOut 
              FROM AccessLog As B  
              WHERE (((B.TerminalID)<>"iGuard1A" 
               And (B.TerminalID)<>"iGuard1B") 
               AND ((B.EmployeeID) Like "2*") 
               AND ((B.InOut)="OUT")) 
               ORDER BY B.EmployeeID, B.LogDate, B.LogTime)  
AS C INNER JOIN Employee ON C.EmployeeID= Employee.EmployeeID
GROUP BY C.EmployeeID, MonthName(Month([LogDate]));