找时差

时间:2013-07-24 19:44:35

标签: sql ms-access

我试图在MS Access中执行以下查询以找到两次之间的差异。时间格式例如是开始时间8:00和结束时间14:00。但是我在查询中遇到错误,请帮我解决。

PARAMETERS [Enter Date] DateTime;

SELECT
    M.CompanyID AS Emp_ID,
    MIN(H.EventTime) AS Attending,
    MAX(H.EventTime) AS Leaving,
    Format( (Leaving-Attending), "hh:mm:ss") AS TotalHrs
FROM
    Member AS M
    LEFT JOIN History AS H ON M.UserID = H.EventUserID 
    AND
    ( Format( [EventDate], "dd/mm/yyyy" ) ) = [Enter Date]    
WHERE
    companyId NOT IN (select companyid from exceptions)
GROUP BY
    M.CompanyID, EventDate
ORDER BY
    CompanyId;

Ayha

2 个答案:

答案 0 :(得分:3)

您可以将SELECT的大部分内容移动到子查询中。然后,从父查询中,您可以使用子查询中定义的名称LeavingAttending来计算它们的差异。

PARAMETERS [Enter Date] DateTime;
SELECT
    sub.Emp_ID,
    sub.Attending,
    sub.Leaving,
    Format((sub.Leaving - sub.Attending), "hh:mm:ss") AS TotalHrs
FROM
    (
        SELECT
            M.CompanyID AS Emp_ID,
            MIN(H.EventTime) AS Attending,
            MAX(H.EventTime) AS Leaving
        FROM
            Member AS M
            LEFT JOIN History AS H
            ON
                M.UserID = H.EventUserID 
            AND Format([EventDate], "dd/mm/yyyy") = [Enter Date]    
        WHERE
            M.companyId NOT IN (select companyid from exceptions)
        GROUP BY
            M.CompanyID, EventDate
    ) AS sub
ORDER BY sub.Emp_ID

大部分繁重都是在子查询中完成的。父查询只是检索子查询值并减去一对值,因此这个建议不应该显着增加额外的性能损失。

答案 1 :(得分:2)

在查询完成之前,Leaving和Attending没有值。因此,Leaving-Attending在处理查询的SELECT部分​​时对Jet没有任何意义。你需要用MIN(H.EventTime)代替Attending和MAX(H.EventTime)来计算TotalHrs的计算。