2012-08-12 40 views
0

我有以下代碼。我的問題是我想從另一個表名Employee中檢索每個員工姓名,該僱員具有與此Access查詢表中類似於AccessLog表的EmployeeID列。現在當我加入這兩張表時,它給了我在標題上所說的奇怪的錯誤。沒有在第一個SELECT中加入的代碼 - A.EmployeeID,Employee.FirstName;作爲內部聯接員工將A.EmployeeID = Employee.EmployeeID添加到最後一個ORDER By子句中,它運行良好。我認爲問題在於命名/別名。有類似的線程,我看到了,但那些沒有解決我的問題。如果有人能指出我的錯誤,將不勝感激。指定的字段可能引用FROM子句中列出的多個表

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])); 

回答

0

我解決了它。正如我所說有名稱/別名問題。我必須給每個子查詢中使用的同一個表分別提供別名。即使在這之後,它顯示了另一個頂級選擇中名字的混合函數的錯誤,我也通過將它包含在子查詢中來解決這個問題。所以在全我的代碼去如下:

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])); 
1

我認爲這可能是GROUP BY EmployeeId - 試圖改變這種要麼GROUP BY A.EmployeeIdGROUP BY Employee.EmployeeId

編輯:它可能必須是GROUP BY A.EmployeeId,因爲這是使用SELECT的那個。

+0

這給了我一個新的錯誤 - 「您試圖執行一個查詢,不包括指定表達式「firstname'as聚合函數」 – 2012-08-12 11:03:00

+1

啊,因爲你「重新計算持續時間,該SELECT語句中的所有內容都需要處於聚合函數或GROUP BY子句中。將Employee.FirstName添加到GROUP BY子句將是解決它的快捷方式,但它可能會產生意想不到的副作用。如果是這樣,你可能需要重構查詢。 – 2012-08-12 11:06:58

+0

是的,這給我數據類型不匹配錯誤。不知道如何解決這個問題,因爲這個領域是新的。 – 2012-08-12 11:13:00

相關問題