计算绩效评估的早晚日期

时间:2019-08-01 17:08:38

标签: sql-server performance syntax datediff

我正在尝试计算SSMS的运输性能-我在几个方面陷入困境,希望我能得到一些帮助!

我有一个预计发货日期,一个申请日期和一个实际发货日期。

我正在衡量仓库的性能,因此在大多数情况下,装运是在同一天离开的。但是在另一些情况下,他们可能会提前几天或延迟几天离开。

我遇到的问题是正确的输出。我想在DD:HH:MM中显示值,但是DateDiff的语法没有给我准确的使用日期:

例如,某批货物本应于2019年6月3日@上午8点离开,但直到7/22/2019 @ 6:30才离开仓库。在这种情况下,DateDiff计算49天,而实际上是48天22小时30分钟。这是我使用的一些语法示例:

  EstimatedShipDate datetime,
  AppointmentShipDate datetime,
  ActualShipDate datetime
  );

insert into #test values ('2019-07-01 11:00', '2019-07-01 11:00','2019-06-30 10:30');
insert into #test values ('2019-07-08 13:45', null,'2019-07-01 22:00');
insert into #test values ('2019-07-09 15:00', null,'2019-07-10 15:00');
insert into #test values ('2019-07-03 15:00', null,'2019-07-04 15:00');
insert into #test values ('2019-07-08 15:00', null,'2019-07-08 15:00');
insert into #test values ('2019-07-08 15:00', null,'2019-07-08 22:00');
insert into #test values ('2019-07-03 08:00', null,'2019-07-04 15:00');
insert into #test values ('2019-07-03 08:00', null,'2019-07-03 06:30');
insert into #test values ('2019-06-03 08:00', null,'2019-07-22 06:30');
insert into #test values ('2019-07-01 11:00', null,'2019-06-29 10:30');


Select 
EstimatedShipDate,
AppointmentShipDate,
ActualShipDate,


DATEDIFF(DAY,ISNULL(CAST(AppointmentShipDate as DateTime),CAST(EstimatedShipDate as DateTime)), CAST(ActualShipDate as DateTime)) as Days,
DATEPART(DAY,  ISNULL(CAST(AppointmentShipDate as DateTime),CAST(EstimatedShipDate as DateTime))-CAST(ActualShipDate as DateTime) ) as days2,
DATEDIFF(Hour,ISNULL(CAST(AppointmentShipDate as DateTime),CAST(EstimatedShipDate as DateTime)), CAST(ActualShipDate as DateTime)) as Hours,
convert(varchar, CAST(ActualShipDate as DateTime)-ISNULL(CAST(AppointmentShipDate as DateTime),CAST(EstimatedShipDate as DateTime)),108) as DateSubtract_Convert,
convert(varchar,ISNULL(CAST(AppointmentShipDate as DateTime),CAST(EstimatedShipDate as DateTime)-CAST(ActualShipDate as DateTime) ),108) as DateSubtract_ConvertEarly
from #TEST

在这种情况下,DateDiff计算49天,而实际上是48天22小时30分钟。反之亦然,我的日期范围显示为2天23:20,但应为1天23:20

2 个答案:

答案 0 :(得分:2)

一种方法是将DATEDIFF的粒度增加到MINUTE。这将需要使用分钟(除法和模数)来计算天,小时和分钟。

SELECT ABS(dT.diff_Minutes / 1440) AS [Days] --days, there are 1440 minutes in a day
      ,ABS((dT.diff_Minutes % 1440) / 60) AS [Hours] --hours remaining in the day
      ,ABS((dT.diff_Minutes % 1440) % 60) AS [Minutes] --minutes remaining in the day

  FROM (
         SELECT DATEDIFF(MINUTE, ISNULL(AppointmentShipDate, EstimatedShipDate), ActualShipDate) [diff_Minutes]
           FROM #test
       ) AS dT

您的临时表产生输出:

Days    Hours   Minutes
 1        0       30
 6       15       45
 1        0        0
 1        0        0
 0        0        0
 0        7        0
 1        7        0
 0        1       30
48       22       30
 2        0       30

将它们放入DD:HH:MM格式比较复杂,但是您可以将数字强制转换为varchar并串联为字符串。 RIGHT用于添加任何前导零。

SELECT CASE WHEN dT.diff_Minutes < 0 THEN '- ' ELSE '+ ' END --positive or negative
      + RIGHT('00' + CAST(ABS(dT.diff_Minutes / 1440) as varchar(1000)), 2) 
      + ':'
      +RIGHT('00' + CAST((ABS(dT.diff_Minutes % 1440) / 60) as varchar(2)), 2) 
      + ':'
      +RIGHT('00' + CAST((ABS(dT.diff_Minutes % 1440) % 60) as varchar(2)), 2)
      AS [DD:HH:MM]

  FROM (
         SELECT DATEDIFF(MINUTE, ISNULL(AppointmentShipDate, EstimatedShipDate), ActualShipDate) [diff_Minutes]                   
           FROM #test
       ) AS dT

产生输出:

DD:HH:MM
- 01:00:30
- 06:15:45
+ 01:00:00
+ 01:00:00
+ 00:00:00
+ 00:07:00
+ 01:07:00
- 00:01:30
+ 48:22:30
- 02:00:30

答案 1 :(得分:0)

用秒而不是天,然后用秒,分钟和小时做一些数学运算。

这是一个简单的SSMS示例:

DECLARE
    @ScheduledDate DATETIME = '6/3/2019 08:00:00', 
    @ShippedDate DATETIME = '07/22/2019 06:30:00';

SELECT
    CONVERT( VARCHAR, DATEDIFF( s, @ScheduledDate, @ShippedDate ) /60/60/24 ) + ' Days and '
    + CONVERT( VARCHAR, ( @ShippedDate - @ScheduledDate ), 108 ) + ' Hours.';

返回

48 Days and 22:30:00 Hours.