gpt4 book ai didi

sql - 寻找时差

转载 作者:行者123 更新时间:2023-12-04 22:41:43 25 4
gpt4 key购买 nike

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

阿哈

最佳答案

您可以将大部分 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

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

关于sql - 寻找时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17843488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com