计算两个时间戳之间的差异

时间:2020-01-30 12:41:27

标签: sql-server datetime

我正在尝试计算两个不同时间之间的差,并将该值分配给先前的状态。因此,如果表中有一行显示上午8点,状态为“饿死”,而下一行显示830上午,状态为“运行中”,那么我需要能够显示第3列来分配30分钟饿到第一列的饿行。那有可能吗。

这是当前2列的图像,我正在尝试计算差异。

This is an image of the current 2 columns and where I am trying to calculate the difference.

2 个答案:

答案 0 :(得分:0)

我尝试了一下。首要工作是使您的数据具有某种结构:

DECLARE @table TABLE (t_stamp DATETIME, SanderRMT_StateChange VARCHAR(50), seconds_between_intervals INT);
INSERT INTO @table SELECT '20200130 06:04:59.267', 'Running', NULL;
INSERT INTO @table SELECT '20200130 06:04:53.260', 'Starved', NULL;
INSERT INTO @table SELECT '20200130 06:04:26.223', 'Running', NULL;
INSERT INTO @table SELECT '20200130 06:04:18.213', 'Starved', NULL;
INSERT INTO @table SELECT '20200130 06:04:13.150', 'Running', NULL;
INSERT INTO @table SELECT '20200130 06:04:11.103', 'Starved', NULL;
INSERT INTO @table SELECT '20200130 06:03:54.073', 'Running', NULL;
INSERT INTO @table SELECT '20200130 06:03:48.067', 'Starved', NULL;
INSERT INTO @table SELECT '20200130 06:03:31.007', 'Running', NULL;
INSERT INTO @table SELECT '20200130 06:03:13.907', 'Starved', NULL;

我认为您要问的是,我们使用元组,每个元组都以“饥饿”行开头,然后我们需要查找下一个“运行”行,计算时间间隔,并将其写回到“饥饿”行。

如果这不是您想要的,那么显示所需的结果集可能会有所帮助?

--Organise the data into timestamp order
WITH x AS (
    SELECT
        *,
        ROW_NUMBER() OVER (ORDER BY t_stamp DESC) AS order_id
    FROM
        @table)
--Update the starved rows with the time intervals
UPDATE
    t2
SET
    seconds_between_intervals = DATEDIFF(SECOND, t2.t_stamp, t1.t_stamp)
FROM
    x x1
    INNER JOIN @table t1 ON t1.t_stamp = x1.t_stamp AND t1.SanderRMT_StateChange = 'Running'
    INNER JOIN x x2 ON x2.order_id = x1.order_id + 1
    INNER JOIN @table t2 ON t2.t_stamp = x2.t_stamp AND t2.SanderRMT_StateChange = 'Starved';

您可能可以使用LAGLEAD来更有效地做到这一点?

结果是:

t_stamp SanderRMT_StateChange   seconds_between_intervals
2020-01-30 06:04:59.267 Running NULL
2020-01-30 06:04:53.260 Starved 6
2020-01-30 06:04:26.223 Running NULL
2020-01-30 06:04:18.213 Starved 8
2020-01-30 06:04:13.150 Running NULL
2020-01-30 06:04:11.103 Starved 2
2020-01-30 06:03:54.073 Running NULL
2020-01-30 06:03:48.067 Starved 6
2020-01-30 06:03:31.007 Running NULL
2020-01-30 06:03:13.907 Starved 18

答案 1 :(得分:-2)

对于MySQL

TIMESTAMPDIFF 是MySQL的本机函数,它返回指定日期部分间隔(秒,天,周等)中两个给定时间戳之间的差(一个时间戳减去另一个时间戳)。返回的值是一个INTEGER,即两个时间戳之间这些间隔的数量。

注意::如果结束日期<开始日期,则 TIMESTAMPDIFF 返回负整数值。)

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

示例:

mysql> SELECT TIMESTAMPDIFF(MINUTE,'2000-02-01 12:05:55 260','2003-05-01 12:05:55 267');
     > 1706400

有关更多信息,您可以参考official docs

对于Transact-SQL

DATEDIFF ( datepart , startdate , enddate )

示例

DECLARE 
    @start_dt DATETIME2= '2019-12-31 23:59:59.9999999', 
    @end_dt DATETIME2= '2020-01-01 00:00:00.0000000';


SELECT 
    DATEDIFF(year, @start_dt, @end_dt) diff_in_year, 
    DATEDIFF(quarter, @start_dt, @end_dt) diff_in_quarter, 
    DATEDIFF(month, @start_dt, @end_dt) diff_in_month, 
    DATEDIFF(dayofyear, @start_dt, @end_dt) diff_in_dayofyear, 
    DATEDIFF(day, @start_dt, @end_dt) diff_in_day, 
    DATEDIFF(week, @start_dt, @end_dt) diff_in_week, 
    DATEDIFF(hour, @start_dt, @end_dt) diff_in_hour, 
    DATEDIFF(minute, @start_dt, @end_dt) diff_in_minute, 
    DATEDIFF(second, @start_dt, @end_dt) diff_in_second, 
    DATEDIFF(millisecond, @start_dt, @end_dt) diff_in_millisecond;

official docs中引用更多