2

Possible Duplicate:
Date difference between two records in same table

I am new to MS Access, Know a little SQL and this is my first question here. before asking question I have searched a lot to find a suitable solution. Here's the scenario:

I have a table with the following structure:
RCDID-- EmployeeID-- LogDate-- LogTime-- terminalID-- InOut
1 -----------2001---------1/4/2012---15:39:27----iGuard3A-----IN
2 -----------2023---------1/4/2012---15:45:27----iGuard3A-----IN
3 -----------2001---------1/4/2012---15:47:29----iGuard3A-----Out

final requirement is to get the total working time for each employee id for every month. main problem is the login and log out time is on different records. but i already did that, i mean i wrote the sql to have another calculated for every login logout time interval. with some other criteria my sql is as follows:

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 AccessLog.TerminalID <> "iGuard1A" 
               AND AccessLog.TerminalID  <> "iGuard1B" 
               AND AccessLog.EmployeeID LIKE "2*"),"hh:nn:ss") AS PrevTime,
       Format((ElapsedTime([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;

It turned out to be a complex query to me. but because this is the first time i m working on access, i wasn't aware about everything from the beginning. I calculated the prevtime and used it as login time for the corresponding logout time records.

Now my goal is to calculate the total duration for each employee id. I used this following code. but it needs the group by clause which i m not sure about.

Format((Sum([Duration])-Int(Sum([Duration]))),"hh:nn:ss") AS TotalTime

this total time will be based on every month.

any help will be appreciated. am i on the wrong path. should i have used vba and report for this purpose?

4

1 に答える 1

0

次のように解決策を見つけましたが、クエリに時間がかかりすぎて、1分近くかかります。何度も回心したせいだと思います。このクエリを最適化するにはどうすればよいですか。時間変換の方が速い関数はどれですか?テーブルのlogtimeのデータ型はdate/timeですが、テキストなどの他の型に変更する必要がありますか?

SELECT EmployeeID,
       month(LogDate), 
       SUM(int(DateDiff("s",  '00:00:00',Duration))) AS abc
       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) 
GROUP BY EmployeeID, month(LogDate);
于 2012-08-11T10:44:36.537 に答える