我有以下代碼。我的問題是我想從另一個表名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]));
這給了我一個新的錯誤 - 「您試圖執行一個查詢,不包括指定表達式「firstname'as聚合函數」 – 2012-08-12 11:03:00
啊,因爲你「重新計算持續時間,該SELECT語句中的所有內容都需要處於聚合函數或GROUP BY子句中。將Employee.FirstName添加到GROUP BY子句將是解決它的快捷方式,但它可能會產生意想不到的副作用。如果是這樣,你可能需要重構查詢。 – 2012-08-12 11:06:58
是的,這給我數據類型不匹配錯誤。不知道如何解決這個問題,因爲這個領域是新的。 – 2012-08-12 11:13:00