Mysql根据时间戳获得两行之间的差异

时间:2017-07-21 19:29:03

标签: mysql group-by

所以我在下面有这些数据

# AgreementID, AssetID, EventDate,               OdometerReading
'247',         '27',    '2017-07-21 15:02:22',   '30'
'247',         '74',    '2017-07-21 14:58:17',   '78'
'247',         '27',    '2017-07-21 14:58:15',   '12'
'247',         '74',    '2017-07-21 14:57:07',   '70'
'247',         '74',    '2017-07-21 14:46:46',   '63'
'247',         '74',    '2017-07-21 14:45:19',   '60'
'247',         '74',    '2017-07-21 14:38:58',   '59'
'247',         '74',    '2017-07-21 14:37:30',   '5'
'245',         '40',    '2017-07-21 11:43:23',   '12'
'245',         '40',    '2017-07-21 11:11:45',   '10'

我要做的是获取每个协议ID的MAX(EventDate)的odometerReading和每个assetID的MIN(EventDate)之间的差异。

结果应如下所示:

# AgreementID, AssetID, OdometerDifference
'247',         '27',    18
'247',         '74',    73
'245',         '40',    2

到目前为止我得到了

 select MAX(T.OdometerReading) - MIN(T.OdometerReading)
 from T
 group by T.agreementID, T.AssetID
 order by T.AgreementID desc

它正在做我想要它做的事情并返回正确的结果,但我希望结果基于max和min eventdate。

2 个答案:

答案 0 :(得分:0)

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(agreementid INT NOT NULL
,assetid INT NOT NULL
,eventdate DATETIME NOT NULL
,odometerreading INT NOT NULL
,PRIMARY KEY(agreementid,assetid,eventdate)
);

INSERT INTO my_table VALUES
(247,27,'2017-07-21 15:02:22',30),
(247,74,'2017-07-21 14:58:17',78),
(247,27,'2017-07-21 14:58:15',12),
(247,74,'2017-07-21 14:57:07',70),
(247,74,'2017-07-21 14:46:46',63),
(247,74,'2017-07-21 14:45:19',60),
(247,74,'2017-07-21 14:38:58',59),
(247,74,'2017-07-21 14:37:30', 5),
(245,40,'2017-07-21 11:43:23',12),
(245,40,'2017-07-21 11:11:45',10);

SELECT a.*
     , mn.odometerreading min_eventdatereading
     , mx.odometerreading max_eventdatereading
     , ABS(mn.odometerreading - mx.odometerreading) diff
  FROM 
     ( SELECT agreementid
            , assetid
            , MIN(eventdate) min_eventdate
            , MAX(eventdate) max_eventdate
         FROM my_table
        GROUP
           BY agreementid
            , assetid
     ) a
  JOIN my_table mn
    ON mn.agreementid = a.agreementid
   AND mn.assetid     = a.assetid
   AND mn.eventdate   = a.min_eventdate
  JOIN my_table mx
    ON mx.agreementid = a.agreementid
   AND mx.assetid     = a.assetid
   AND mx.eventdate   = a.max_eventdate;

+-------------+---------+---------------------+---------------------+----------------------+----------------------+------+
| agreementid | assetid | min_eventdate       | max_eventdate       | min_eventdatereading | max_eventdatereading | diff |
+-------------+---------+---------------------+---------------------+----------------------+----------------------+------+
|         245 |      40 | 2017-07-21 11:11:45 | 2017-07-21 11:43:23 |                   10 |                   12 |    2 |
|         247 |      27 | 2017-07-21 14:58:15 | 2017-07-21 15:02:22 |                   12 |                   30 |   18 |
|         247 |      74 | 2017-07-21 14:37:30 | 2017-07-21 14:58:17 |                    5 |                   78 |   73 |
+-------------+---------+---------------------+---------------------+----------------------+----------------------+------+

答案 1 :(得分:-1)

这可能有用。它自己加入以获取按日期和资产ID的最小和最大行数。

SELECT 
    a.AssetID,
    MIN(a.EventDate) as `start`,
    MAX(a.EventDate) as `end`,
    b.OdometerReading as `start_odo`,
    c.OdometerReading as `end_odo`,
    c.OdometerReading - b.OdometerReading as `distance`
FROM table a
JOIN table b
ON a.AssetID = b.AssetID AND
    MIN(a.EventDate) = b.EventDate
JOIN table c
ON a.AssetID = c.AssetID AND
    MAX(a.EventDate) = c.EventDate
GROUP BY a.AssetID
ORDER BY a.AssetID