gpt4 book ai didi

SQL查询创建计算字段

转载 作者:太空狗 更新时间:2023-10-30 01:57:45 25 4
gpt4 key购买 nike

我有一个这样的数据库表:
enter image description here
我希望我能很好地解释这一点,以便您能理解。

我想计算每个员工工作了多少小时。
例如,对于“Arjeta Domi”,我们有 Cell(2,3) - Cell(3,3) + Cell(4,3) + Cell(5,3),使每个注销时间与登录时间不同。

enter image description here

我想要的最终表将包含这些列:CardNoUserNameDatePauseTime工作时间

我试过这个查询:取自 duplicate

SELECT DISTINCT
[Card NO],
[User Name],
(
SELECT
MIN(DateTime) AS [Enter Time],
MAX(DateTime) AS [Exit Time],
MAX(DateTime) - MIN(DateTime) AS [Inside Hours]
FROM
ExcelData
)
FROM
ExcelData
GROUP BY
[Card NO], [User Name], DateTime

DateTime 列的类型是 String,而不是 DateTime。我正在使用 MS Access 数据库。

最佳答案

选择所有包含 'm001-1-In' 且 DateTime 为 I 的行,并将合适的 'm001-1-Exit' 行添加到其中,子查询为 O,这将如下所示:

SELECT t1.[Card No], t1.[User Name],dateTime as I
,(Select TOP 1 dateTime from Tab t2 where t2.[Card No]= t1.[Card No]
and t2.[User Name]= t1.[User Name] and t2.Addr='m001-1-Exit'
and t2.DateTime>t1.datetime ORDER by DateTime) as O
FROM Tab t1
where t1.Addr='m001-1-In'

enter image description here

现在很容易封装它,如下所示 Prepared 并将我们的 SUM 和 Grouping 添加到此:

SELECT [Prepared].[Card No], [Prepared].[User Name], SUM(DateDiff('n',I,O))/60 AS Hours
FROM (
SELECT t1.[Card No], t1.[User Name],dateTime as I
,(Select TOP 1 dateTime from Tab t2 where t2.[Card No]= t1.[Card No]
and t2.[User Name]= t1.[User Name] and t2.Addr='m001-1-Exit'
and t2.DateTime>t1.datetime ORDER by DateTime) as O
FROM Tab t1
where t1.Addr='m001-1-In'
) AS [Prepared]
GROUP BY [Prepared].[Card No], [Prepared].[User Name]

如果您需要限制 DateRange,请将所需的条件添加到行 where t1.Addr='m001-1-In'

关于SQL查询创建计算字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915156/

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