3

我正在尝试在 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;

啊哈

4

2 回答 2

3

您可以将其中的大部分内容SELECT移到子查询中。然后从父查询中,您可以使用子查询中定义的名称,Leaving并且Attending当您计算它们的差异时。

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

大多数繁重的工作都是在子查询中完成的。父查询只是检索子查询值并减去一对值,因此该建议不应带来显着的额外性能损失。

于 2013-07-24T21:16:40.883 回答
2

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

于 2013-07-24T20:05:41.003 回答