如何使用DATEDIFF查找行之间的分钟差异?

时间:2014-11-20 03:39:42

标签: mysql sql-server

我发现了许多以前的主题,例如:

SQL Datediff - find datediff between rows SQL query to get DateDiff of the last two records

然而,我无法理解这是如何运作的。我不是高级或SQL专家,所以这让我很烦。我在小提琴上创建了以下内容:

CREATE TABLE activity
(
  IDOrder int,
  IDActivity int,
  DateActivity DateTime
);

INSERT INTO activity (IDOrder, IDActivity, DateActivity) VALUES 
(1, 1, "2007-04-16 08:34:00"),
(1, 2, "2007-04-16 09:22:00"), 
(1, 3, "2007-04-16 09:51:00"),
(1, 4, "2007-04-16 16:14:00"), 
(2, 1, "2007-04-16 08:34:00"),
(3, 1, "2007-04-16 08:34:00"), 
(3, 2, "2007-04-16 09:22:00"),
(3, 3, "2007-04-16 09:51:00"), 
(3, 4, "2007-04-16 16:14:00"),
(4, 1, "2007-04-16 08:34:00"), 
(4, 2, "2007-04-16 09:22:00"),
(4, 3, "2007-04-16 09:51:00");

INSERT INTO activity (IDOrder, IDActivity , DateActivity) VALUES 
(2, 2, CURRENT_TIMESTAMP), 
(2, 3, CURRENT_TIMESTAMP);

有人能帮我推动正确的方向吗?我想要的输出是4列,这些差异在几分钟内。时间1是IDActivity 1和2之间的差异。时间2是IDActivity 2和3之间的差异。时间3介于3和4之间,时间4介于4和1之间。在IDActivity不存在的某些情况下,I希望它返回null。

我最好的想法是使用IF THEN语句,一个巨大的循环返回4列数据,但我不确定如何实现它。

2 个答案:

答案 0 :(得分:0)

试试这个:

专栏" timeMinute" IDActivity no x和no x + 1之间的分钟差异 在同一个" IDOrder"。

Select a.*, datediff(minute, a.DateActivity, b.DateActivity) as timeMinute
From Activity a
Left Join Activity b
    On b.IDOrder = a.IDOrder
    And b.IDActivity-1 = a.IDActivity
Order By a.IDOrder, a.IDActivity

答案 1 :(得分:0)

通过考虑案例陈述,你走在正确的轨道上。我将以与解决它相同的方式引导您完成此事。

我首先编写了一个使用case语句的select查询来获取第一,第二,第三和第四个活动日期:

SELECT idorder, 
   (CASE WHEN idactivity = 1 THEN dateactivity ELSE NULL END) AS firstDate,
   (CASE WHEN idactivity = 2 THEN dateactivity ELSE NULL END) AS secondDate,
   (CASE WHEN idactivity = 3 THEN dateactivity ELSE NULL END) AS thirdDate,
   (CASE WHEN idactivity = 4 THEN dateactivity ELSE NULL END) AS fourthDate
FROM activity;

但是,这会为每个组返回一个丑陋的四行结果集。第一行具有firstDate值,其余为null。第二行具有secondDate值,对于第一,第三和第四个日期为null。请进一步查看SQL Fiddle以获取图像。

所以,我使用它作为子查询并使用MAX()函数获取每个日期,每个idorder值有一行:

SELECT idorder,
  MAX(firstDate) AS firstDate,
  MAX(secondDate) AS secondDate,
  MAX(thirdDate) AS thirdDate,
  MAX(fourthDate) AS fourthDate
FROM(SELECT idorder, 
     (CASE WHEN idactivity = 1 THEN dateactivity ELSE NULL END) AS firstDate,
     (CASE WHEN idactivity = 2 THEN dateactivity ELSE NULL END) AS secondDate,
     (CASE WHEN idactivity = 3 THEN dateactivity ELSE NULL END) AS thirdDate,
     (CASE WHEN idactivity = 4 THEN dateactivity ELSE NULL END) AS fourthDate
      FROM activity) t
GROUP BY idorder;

然后,我能够修改上面的查询以使用MySQL中的TIMESTAMPDIFF()函数来获得分钟的差异。你仍然需要计算MAX,然后只比较第一个和第二个的最大值,以及第三个和第四个的最大值。它看起来像这样:

SELECT idorder,
  TIMESTAMPDIFF(minute, MAX(firstDate), MAX(secondDate)) AS firstTimeDifference,
  TIMESTAMPDIFF(minute, MAX(thirdDate), MAX(fourthDate)) AS secondTimeDifference
FROM(SELECT idorder, 
     (CASE WHEN idactivity = 1 THEN dateactivity ELSE NULL END) AS firstDate,
     (CASE WHEN idactivity = 2 THEN dateactivity ELSE NULL END) AS secondDate,
     (CASE WHEN idactivity = 3 THEN dateactivity ELSE NULL END) AS thirdDate,
     (CASE WHEN idactivity = 4 THEN dateactivity ELSE NULL END) AS fourthDate
      FROM activity) t
GROUP BY idorder;

这是SQL Fiddle

修改

上述内容并未返回您需要的所有四列,但希望您具备获取其他值所需的技能。