从SQL Server中的多个时钟输入或多个时钟输出中获取时钟输入和时钟输出时间

时间:2019-05-23 06:44:31

标签: sql sql-server

我正在使用Web应用程序来管理员工的上班/下班时间,并按计划的班次进行映射。

注意LogType 1表示时钟输入,2表示时钟输出。

我的登录表结构如下

UserId       LogDateTime             LogType

 5005    2019-05-20 21:35:48.490        1
 5005    2019-05-20 22:25:00.000        1
 5005    2019-05-21 06:48:00.000        2
 5005    2019-05-21 07:01:15.383        2

 5006    2019-05-20 21:25:25.470        1
 5006    2019-05-20 23:48:29.568        2
 5006    2019-05-21 00:07:05.056        1
 5006    2019-05-21 07:25:35.853        2

 5007   2019-05-20 23:33:35.017         1
 5007   2019-05-21 00:18:56.087         2
 5007   2019-05-21 09:01:23.577         2

我想以以下格式输入和输出时钟,并将其用于计划的班次映射

UserId      Date            ClockIn                    ClockOut
5005      2019-05-20     2019-05-20 21:35:48.490     2019-05-21 07:01:15.383
5006      2019-05-20     2019-05-20 21:25:25.470     2019-05-21 07:25:35.853
5007      2019-05-21     2019-05-20 23:33:35.017     2019-05-21 09:01:23.577 

任何人都可以共享该查询。

2 个答案:

答案 0 :(得分:0)

尝试一下,<myTable>需要替换为表的实际名称:

SELECT 
    UserId,
    CONVERT(CHAR(10), LogDateTime, 126) AS Date,
    LogDateTime AS ClockIn,
    -- this sub query will by using WHERE clause, ORDER BY, TOP 1
    -- get the closest CheckOut time for the same user
    (
        SELECT TOP 1 LogDateTime 
        FROM <myTable> AS t2 
        WHERE t2.UserId = t1.UserId 
              AND t2.LogDateTime > t1.LogDateTime
              AND LogType = 2
        ORDER BY LogDateTime ASC
    )  AS ClockOut
FROM <myTable> AS t1 
WHERE LogType = 1
    -- this sub query is to limit the result to only the latest CheckIn times
    AND t1.LogDateTime IN
    (
        SELECT MAX(t3.LogDateTime)
        FROM <myTable> AS t3
        WHERE t3.UserId = t1.UserId
          AND LogType = 1
    ) 

希望有帮助!

答案 1 :(得分:0)

我假设Date列后面的逻辑正在考虑用户分配签入和签出的日期,如果用户仅分配签入或仅签出记录将被忽略(在实际生活中,可能会发生用户忘记了)

无论如何要实现的目标都应该使用MIN()MAX()Group ByHaving作为下一个

我认为我的代码更好,但至少我的代码可以满足需求

演示:-

Create table #MyTempTable (UserId int , LogDateTime Datetime,LogType int)

Insert into #MyTempTable values (5005,'2019-05-20 21:35:48.490',1)
Insert into #MyTempTable values (5005,'2019-05-20 22:25:00.000',1)
Insert into #MyTempTable values (5005,'2019-05-21 06:48:00.000',2)
Insert into #MyTempTable values (5005,'2019-05-21 07:01:15.383',2)
Insert into #MyTempTable values (5006,'2019-05-20 21:25:25.470',1)
Insert into #MyTempTable values (5006,'2019-05-20 23:48:29.568',2)
Insert into #MyTempTable values (5006,'2019-05-21 00:07:05.056',1)
Insert into #MyTempTable values (5006,'2019-05-21 07:25:35.853',2)
Insert into #MyTempTable values (5007,'2019-05-20 23:33:35.017',1)
Insert into #MyTempTable values (5007,'2019-05-21 00:18:56.087', 2)
Insert into #MyTempTable values (5007,'2019-05-21 09:01:23.577',2)

select UserId,min(Date) Date ,min(ClockIn) ClockIn ,Max(ClockOut) ClockOut 
from (
    select UserId, Min(convert(date,LogDateTime)) Date, 
    Min(LogDateTime) ClockIn , Max(LogDateTime) ClockOut,LogType
    from #MyTempTable
    Group by UserId,LogType
    having count(convert(date,LogDateTime)) > 1
    )t
Group by UserId

Drop table #MyTempTable

结果:-

UserId  Date        ClockIn                 ClockOut
5005    2019-05-20  2019-05-20 21:35:48.490 2019-05-21 07:01:15.383
5006    2019-05-20  2019-05-20 21:25:25.470 2019-05-21 07:25:35.853
5007    2019-05-21  2019-05-21 00:18:56.087 2019-05-21 09:01:23.577