查找一天中花费的时间以及休息时间

时间:2018-08-09 07:05:12

标签: sql sql-server sql-server-2014

在这种情况下,我需要找出在办公室进行一些内部申请所花费的总时间。

我有这样的示例数据:

Id  EmployeeId  ScanDateTime    Status
 7  87008   2018-08-02 16:03:00.227 1
 8  87008   2018-08-02 16:06:17.277 2
 9  87008   2018-08-02 16:10:37.107 3
 10 87008   2018-08-02 16:20:17.277 2
 11 87008   2018-08-02 16:30:37.107 3
12  87008   2018-08-02 20:06:00.000 4

此处的状态含义不同:

1-开始 2-暂停 3-恢复 4-结束

意味着员工在状态为1时在ScanDateTime开始工作。他们可以休息一下(状态2),然后返回并继续工作(状态3),状态为4表示他们正在结束工作。 注意:工作时间可能会有多个休息时间。

预期输出:

EmployeeId  StartTime                 EndTime                  BreakInMins 
87008       2018-08-02 16:03:00.227   2018-08-02 20:06:00.000   14

我试图按照一些示例来计算预期结果集,但没有帮助。

在类似的示例可用的地方,我找不到任何这样的示例。

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

请尝试这个。当休息仍在进行中或会话未完成时,处理多个休息/员工和案件

select
     [EmployeeId]   =   [s].[EmployeeId]
    ,[StartTime]    =   [s].[ScanDateTime]
    ,[EndTime]      =   [et].[ScanDateTime]
    ,[BreakInMins]  =   [b].[BreakInMins]
from
    [Scans] as  [s] --  here is your table
outer apply
    (      
        select top 1 [ScanDateTime], [Id] from [Scans] where [Id] > [s].[Id] and [EmployeeId] = [s].[EmployeeId] and [Status] = 4 order by [ScanDateTime] asc
    )       as  [et]
outer apply
    (
        select
              [BreakInMins] = sum(isnull([r].[mins], datediff(mi, [sp].[ScanDateTime], getdate())))
        from
            [Scans] as [sp]
        outer apply
            (
                select top 1 [mins] = datediff(mi, [sp].[ScanDateTime], [ScanDateTime]) from [Scans] where [Id] > [sp].[Id] and [EmployeeId] = [sp].[EmployeeId] and [Status] IN (3, 4) order by [ScanDateTime] asc
            ) as [r]
        where
                [sp].[id] > [s].[id] and [sp].[id] < isnull([et].[id], [id] + 1)
            and [sp].[EmployeeId] = [s].[EmployeeId]
            and [sp].[Status] = 2

    )       as  [b]    
where
        [Status] = 1;

这是易于测试的脚本:script

答案 1 :(得分:1)

您可以尝试一下。 用row_numberCTE中创建一个Status,因为我们需要知道哪个暂停时间对应于哪个恢复时间。然后在{{1}中的self joinCTE

EmployeeId

查询1

CREATE TABLE T(
   Id  INT,
   EmployeeId INT,
   ScanDateTime DATETIME,
  Status INT
);


INSERT INTO T VALUES (7 ,87008 ,'2018-08-02 16:03:00.227',1);
INSERT INTO T VALUES (8 ,87008 ,'2018-08-02 16:06:17.277',2);
INSERT INTO T VALUES (9 ,87008 ,'2018-08-02 16:10:37.107',3);
INSERT INTO T VALUES (10,87008 ,'2018-08-02 16:20:17.277',2);
INSERT INTO T VALUES (11,87008 ,'2018-08-02 16:30:37.107',3);
INSERT INTO T VALUES (12,87008 ,'2018-08-02 20:06:00.000',4);

Results

;with cte as(
  SELECT *,
         MIN(ScanDateTime) over(partition by EmployeeId order by EmployeeId) StartTime,
         MAX(ScanDateTime) over(partition by EmployeeId order by EmployeeId) EndTime,
         ROW_NUMBER() OVER(PARTITION BY Status order by id) rn
  FROM t
)
select t1.EmployeeId,
       t1.StartTime,
       t1.EndTime,
       SUM(datediff(minute,t1.ScanDateTime,t2.ScanDateTime)) BreakInMins 
from 
cte t1 
inner join cte t2
on t1.rn =t2.rn and t1.Status = 2 and t2.Status = 3 and t1.EmployeeId = t2.EmployeeId
group by t1.EmployeeId,
       t1.StartTime,
       t1.EndTime

编辑

如果数据中的日期不同,则可以尝试执行此查询。只需按| EmployeeId | StartTime | EndTime | BreakInMins | |------------|----------------------|--------------------------|-------------| | 87008 | 2018-08-02T20:06:00Z | 2018-08-02T16:03:00.227Z | 14 | 分组即可。

date

查询1

CREATE TABLE T(
   Id  INT,
   EmployeeId INT,
   ScanDateTime DATETIME,
  Status INT
);


INSERT INTO T VALUES (7 ,87008 ,'2018-08-02 16:03:00.227',1);
INSERT INTO T VALUES (8 ,87008 ,'2018-08-02 16:06:17.277',2);
INSERT INTO T VALUES (9 ,87008 ,'2018-08-02 16:10:37.107',3);
INSERT INTO T VALUES (10,87008 ,'2018-08-02 16:20:17.277',2);
INSERT INTO T VALUES (11,87008 ,'2018-08-02 16:30:37.107',3);
INSERT INTO T VALUES (12,87008 ,'2018-08-02 20:06:00.000',4);
INSERT INTO T VALUES (27 ,87008 ,'2018-08-03 16:03:00.227',1);
INSERT INTO T VALUES (28 ,87008 ,'2018-08-03 16:06:17.277',2);
INSERT INTO T VALUES (29 ,87008 ,'2018-08-03 16:11:37.107',3);
INSERT INTO T VALUES (210,87008 ,'2018-08-03 16:20:17.277',2);
INSERT INTO T VALUES (211,87008 ,'2018-08-03 16:30:37.107',3);
INSERT INTO T VALUES (212,87008 ,'2018-08-03 20:06:00.000',4);

Results

;with cte as(
  SELECT EmployeeId,
         MAX(CASE WHEN Status = 1 then ScanDateTime end) StartTime,
         MIN(CASE WHEN Status = 4 then ScanDateTime end) EndTime,
         CAST(ScanDateTime as date) dt
  FROM t
  GROUP BY EmployeeId,CAST(ScanDateTime as date)
)
,cte2 as(
  SELECT t2.*,
         Row_number() over(partition by t2.EmployeeId,t2.Status order by Id) rn,
         t1.StartTime,
         t1.EndTime,
         t1.dt
  FROM cte t1 
  INNER JOIN T t2 ON t1.EmployeeId = t2.EmployeeId and Status in (2,3) and t1.dt =  CAST(t2.ScanDateTime as date)
)
select t1.EmployeeId,
       t1.StartTime,
       t1.EndTime,
       SUM(datediff(minute,t1.ScanDateTime,t2.ScanDateTime)) BreakInMins 
from cte2 t1 
inner join cte2 t2 on 
  t1.rn = t2.rn 
and 
  t1.EmployeeId = t2.EmployeeId
and t1.Status = 2 and t2.Status =3
group by t1.EmployeeId,
       t1.StartTime,
       t1.EndTime

答案 2 :(得分:1)

我考虑每天为员工休息的时间,您可以在下面查看,我还提供了小提琴链接

select t1.*,t5.breakmins from
(
  select EmployeeId,min(StartTime) as StartTime,max(EndTime) as EndTime from 
(
  select EmployeeId,(case when status=1 then ScanDateTime end) as StartTime,
(case when status=4 then ScanDateTime end) as EndTime,
case when status=3 then ScanDateTime end as ResumeWork,
             case when status=2 then ScanDateTime end as pauseTime
    from emp

    ) as t group by EmployeeId
  )  t1
inner join
(
select EmployeeId, convert(date,ResumeWork) as day ,
 sum(case when status=2 then datediff(minute,ResumeWork,res)  end ) as breakmins from
(
select EmployeeId,ResumeWork,status ,
lag(ResumeWork) over(PARTITION BY EmployeeId order by ResumeWork desc) as res from 
(
 select * from 
(
select EmployeeId, case when status=3 then ScanDateTime end as ResumeWork,status from emp
) as t1 where ResumeWork is not null


    union all 
    select * from 
(
    select EmployeeId,case when status=2 then ScanDateTime end as pauseTime,status from emp
  ) as t2 where pauseTime is not null
 )  as t3 group by EmployeeId,ResumeWork,status
  ) t4 group by EmployeeId, convert(date,ResumeWork)
  )t5 on t1.EmployeeId=t5.EmployeeId
  and  convert(date,t1.StartTime)=t5.day


EmployeeId     StartTime                  EndTime          breakmins
87008   2018-08-02T16:03:00.227Z    2018-08-02T20:06:00Z    12

http://sqlfiddle.com/#!18/ae60f/6

答案 3 :(得分:0)

请尝试以下查询:http://sqlfiddle.com/#!18/6fe11/3

   select id,min(case when status=1 then stattime end) as starttime,
min(case when status=4 then stattime end) as endtime,
sum(case when status=2 then minute end) as breakinmin
from
(
select id,stattime,status,
DATEdiff(minute,stattime,lead(stattime,1,NULL) 
         over (partition by id ORDER BY stattime)) as minute
    from ForgeRock)a
    group by id

id      starttime                   endtime                 breakinmin
87008   2018-08-02T16:03:00.227Z    2018-08-02T20:06:00Z    14