在同一列中的两行中两次之间的差异 - MySql

时间:2014-02-02 04:56:50

标签: mysql time diff

如何制作以下内容

表bestellungen

id      abholdatum         abholzeit

1       2014-02-03         03:35:00
2       2014-02-03         08:30:00
3       2014-02-03         05:10:00
4       2014-02-03         15:25:00
5       2014-02-03         11:50:00

我想要这个结果

id      abholdatum         abholzeit endzeit       diff

1       2014-02-03         03:35:00  05:10:00      5700
3       2014-02-03         05:10:00  08:30:00      12000
2       2014-02-03         08:30:00  11:50:00      12000
5       2014-02-03         11:50:00  15:25:00      12900
4       2014-02-03         15:25:00  00:00:00      0

有人可以告诉我如何解决这个问题

THX

3 个答案:

答案 0 :(得分:1)

首先,您需要按如下方式制作数据透视表。

您可以在这里测试http://www.sqlfiddle.com/#!2/d62d4/3

SELECT x.id, x.abholdatum, x.abholzeit AS from_ts, y.abholzeit AS to_ts
FROM (
    SELECT @seq := @seq + 1 AS ord, id, abholdatum, abholzeit
    FROM tbl, (SELECT @seq := 0) init
    ORDER BY abholzeit
) x LEFT JOIN  (

    SELECT @seq2 := @seq2 + 1 AS ord, id, abholdatum, abholzeit
    FROM tbl, (SELECT @seq2 := 0) init
    ORDER BY abholzeit
) y ON x.ord = y.ord - 1;
+------+------------+----------+----------+
| id   | abholdatum | from_ts  | to_ts    |
+------+------------+----------+----------+
|    1 | 2014-02-03 | 03:35:00 | 05:10:00 |
|    3 | 2014-02-03 | 05:10:00 | 08:30:00 |
|    2 | 2014-02-03 | 08:30:00 | 11:50:00 |
|    5 | 2014-02-03 | 11:50:00 | 15:25:00 |
|    4 | 2014-02-03 | 15:25:00 | NULL     |
+------+------------+----------+----------+
5 rows in set (0.00 sec)

第二次,让我们计算时间字段的差异。

SELECT x.id, x.abholdatum, x.abholzeit AS from_ts, y.abholzeit AS to_ts,
TO_SECONDS(CONCAT(x.abholdatum, ' ', y.abholzeit)) - TO_SECONDS(CONCAT(x.abholdatum, ' ', x.abholzeit)) AS diff_ts
FROM (
    SELECT @seq := @seq + 1 AS ord, id, abholdatum, abholzeit
    FROM tbl, (SELECT @seq := 0) init
    ORDER BY abholzeit
) x LEFT JOIN  (

    SELECT @seq2 := @seq2 + 1 AS ord, id, abholdatum, abholzeit
    FROM tbl, (SELECT @seq2 := 0) init
    ORDER BY abholzeit
) y ON x.ord = y.ord - 1;
+------+------------+----------+----------+---------+
| id   | abholdatum | from_ts  | to_ts    | diff_ts |
+------+------------+----------+----------+---------+
|    1 | 2014-02-03 | 03:35:00 | 05:10:00 |    5700 |
|    3 | 2014-02-03 | 05:10:00 | 08:30:00 |   12000 |
|    2 | 2014-02-03 | 08:30:00 | 11:50:00 |   12000 |
|    5 | 2014-02-03 | 11:50:00 | 15:25:00 |   12900 |
|    4 | 2014-02-03 | 15:25:00 | NULL     |    NULL |
+------+------------+----------+----------+---------+
5 rows in set (0.00 sec)

答案 1 :(得分:0)

如果您的所有行都在同一日期,则代码如下所示:

SELECT id, abholdatum, abholzeit, MIN(endzeit) AS endzeit, TIMESTAMPDIFF(SECOND, MIN(endzeit), abholzeit) AS diff
   FROM bestellungen AS early
LEFT JOIN (
    SELECT abholzeit AS endzeit FROM bestellungen
) AS later
WHERE endzeit > abholzeit
GROUP BY id
ORDER BY abholzeit

我要做的是加入表中任何内容的所有组合,但将其限制为第二次(endzeit)大于第一次(abholzeit)的情况。然后我按id分组,这样每个开始时间只有一个条目。

然后我使用MIN(endzeit)来获得结束时间的最低值(必须大于开始时间。)然后我使用函数TIMESTAMPDIFF来获得秒的差异。不太确定最后一次会怎样。

现在它会在不同的日期打破结果,但是你的例子只有一个日期。你需要做一些不同日期的工作,因为有很多不同的组合,其中日期较早,但时间较晚,等等。

如果日期和时间在一列中,则不同日期会更容易,那么您可以使用上述解决方案。这是一个(不完全实用的)不同日期的示例:

SELECT id, abholdatum, abholzeit, MIN(endzeit) AS endzeit, TIMESTAMPDIFF(SECOND, MIN(endzeit), abholzeit) + TIMESTAMPDIFF(SECOND, abholdatum, enddate) AS diff
   FROM bestellungen AS early
LEFT JOIN (
    SELECT abholzeit AS endzeit, abholdatum AS enddate FROM bestellungen
) AS later
WHERE (endzeit > abholzeit
    AND enddate = abholdatum)
    OR enddate > abholdatum
GROUP BY id
ORDER BY abholdatum, abholzeit

警告,最后一个代码仍然真的坏了。例如,它可以从MIN(endzeit)获得一个时间,这是最短的时间,但是从一个非常遥远的日期。将涉及实际的解决方案。如果可能,我建议将日期和时间合并为一列。

答案 2 :(得分:0)

InoS Heo,你的版本有效!!

谢谢......

这就是我需要的:D

SELECT x.id, x.abholdatum, x.abholzeit AS from_ts, y.abholzeit AS to_ts,
TIME_TO_SEC(CONCAT(x.abholdatum, ' ', y.abholzeit)) - TIME_TO_SEC(CONCAT(x.abholdatum, ' ', x.abholzeit)) AS diff_ts
FROM (
    SELECT @seq := @seq + 1 AS ord, id, abholdatum, abholzeit
    FROM bestellungen, (SELECT @seq := 0) init
    WHERE (abholdatum = DATE_SUB(CURDATE(),INTERVAL -1 DAY)) 
    ORDER BY abholzeit
) x LEFT JOIN  (

    SELECT @seq2 := @seq2 + 1 AS ord, id, abholdatum, abholzeit
    FROM bestellungen, (SELECT @seq2 := 0) init
    WHERE (abholdatum = DATE_SUB(CURDATE(),INTERVAL -1 DAY)) 
    ORDER BY abholzeit
) y ON x.ord = y.ord - 1