0

如何按每条记录的分钟和秒获取时间,然后求和

看看它目前的样子以及我希望它如何出现的图像。

谢谢

DECLARE @TimeInSeconds INT 

SET @TimeInSeconds = (SELECT Datediff(second, Min(lra.time), Max(lra.time)) 
                             segundos 
                      FROM   log_rpa lra 
                      WHERE  1 = 1 
                             AND date = Cast(Getdate() AS DATE) 
                      -- Ejecución hoy 
                      GROUP  BY lra.log_id) 

SELECT lr.log_id, 
       lr.project, 
       Datediff(minute, Min(lr.time), Max(lr.time)) Tiempo_ejecucion_minutos, 
       Datediff(second, Min(lr.time), Max(lr.time)) segundos, 
       (SELECT RIGHT('0' + Cast(@TimeInSeconds / 3600 AS VARCHAR), 2) 
               + ':' 
               + RIGHT('0' + Cast((@TimeInSeconds / 60) % 60 AS VARCHAR), 2) 
               + ':' 
               + RIGHT('0' + Cast(@TimeInSeconds % 60 AS VARCHAR), 2)) 
                                                    Tiempo_Ejecucion_exacta 
FROM   log_rpa lr 
WHERE  1 = 1 
GROUP  BY lr.log_id, 
          lr.project, 
          lr.date 
ORDER  BY lr.date DESC 

是这样吗

我需要它

如果我需要将 DurationTime 值作为总和进行总计?

4

2 回答 2

0

看起来你被困在持续时间格式上。该@TimeInSeconds变量是一个常数(其值在初始 之后不会改变SET),因此会产生常数结果 ( 00:05:10)。

下面的解决方案采用以秒为单位的时间差 ( datediff(SS, ...)) 并将这些秒数添加到一个空值中datetime(等于1900-01-01 00:00:00.000,如代码中一样缩写00:00:00)。将其转换回类型time会删除日期部分。

样本数据

create table MyLog
(
  ProjId int,
  LogId int,
  LogDate date,
  LogTime time
);

insert into MyLog (ProjId, LogId, LogDate, LogTime) values
(100, 101, '2020-09-16', '01:00:00'),
(100, 101, '2020-09-16', '01:01:05'), -- project 100, task 101 takes 00:01:05
(100, 102, '2020-09-16', '02:00:00'),
(100, 102, '2020-09-16', '02:05:00'),
(100, 102, '2020-09-16', '02:08:00'); -- project 100, task 102 takes 00:08:00

解决方案

select ml.ProjId,
       ml.LogId,
       ml.LogDate,
       min(ml.LogTime) as LogTimeFrom,
       max(ml.LogTime) as LogTimeTo,
       datediff(SS, min(ml.LogTime), max(ml.LogTime)) as DurationSec,
       convert(time, dateadd(SS, datediff(SS, min(ml.LogTime), max(ml.LogTime)), '00:00:00')) as DurationTime
from MyLog ml
group by ml.ProjId,
         ml.LogId,
         ml.LogDate
order by ml.ProjId,
         ml.LogId;

结果

ProjId  LogId  LogDate     LogTimeFrom  LogTimeTo  DurationSec  DurationTime
------- ------ ----------- ------------ ---------- ------------ -------------
100     101    2020-09-16  01:00:00     01:01:05   65           00:01:05
100     102    2020-09-16  02:00:00     02:08:00   480          00:08:00

在行动中看到它:小提琴

于 2020-09-16T09:38:02.733 回答
0

我正在写一个答案,而 Sander 在我之前回答了!非常相似的答案,但他更简洁。

Sander 解决方案的唯一小问题(也存在于原始问题中)是GROUP BY ml.LogDate. 如果日志在午夜之前开始并在午夜之后结束,您可能会得到两行 - 这可能有 0 时间反对他们(一些其他解决方案可能有负时间,例如,第二天结果的 23:55:00 到 00:05:00在-1430)

相反,我建议计算完整的日期时间,CAST([ml.LogDate] AS datetime) + CAST([ml.LogTime] AS datetime)例如

  • 将其用于 DATEDIFF
  • 使用最大(或最小)日期而不是分组依据

这是 Sander 的代码版本,已针对按日期分组的问题进行了调整

select ml.ProjId,
       ml.LogId,
       max(ml.LogDate) AS LogDate,
       min(ml.LogDatetime) as LogTimeFrom,
       max(ml.LogDatetime) as LogTimeTo,
       datediff(SS, min(ml.LogDateTime), max(ml.LogDateTime)) as DurationSec,
       convert(time, dateadd(SS, datediff(SS, min(ml.LogDateTime), max(ml.LogDateTime)), '00:00:00')) as DurationTime
from (select *,  CAST(LogDate AS datetime) + CAST(logTime AS datetime) AS LogDatetime FROM MyLog) ml
group by ml.ProjId,
         ml.LogId
order by ml.ProjId,
         ml.LogId;

这是对 Sander 小提琴的调整,还有一个额外的日志穿过午夜:fiddle

于 2020-09-16T10:07:15.260 回答