如何在同一日期的同一列中减去两行?

时间:2019-01-18 08:09:01

标签: mysql sql date

我有一个查询。我想在同一天减去第一行和最后一行。我写了这个查询,但是我不确定性能。有替代方法解决此问题吗?

| imei  |    date                 | km    |
|-----------------------------------------|
| 123   | 2019-01-15 00:00:01     | 15    |
| 123   | 2019-01-15 12:12:08     | 8     |
| 123   | 2019-01-15 23:00:59     | 30    |
| 456   | 2019-01-15 00:03:12     | 232   |
| 456   | 2019-01-15 07:04:00     | 123   |
| 456   | 2019-01-15 23:16:18     | 464   |

我的查询:

SELECT 
gg.imei,
DATE_FORMAT(gg.datee, '%Y-%m-%d'),
gg.km - (SELECT 
        g.km
    FROM
        gps g
    WHERE
        g.datee LIKE '2019-01-15%'
            AND g.datee = (SELECT 
                MIN(t.datee)
            FROM
                gps t
            WHERE
                t.datee LIKE '2019-01-15%'
                    AND t.imei = g.imei)
            AND g.imei = gg.imei
    GROUP BY g.imei) AS km
FROM
    gps gg
WHERE
gg.datee LIKE '2019-01-15%'
    AND gg.datee = (SELECT 
        MAX(ts.datee)
    FROM
        gps ts
    WHERE
        ts.datee LIKE '2019-01-15%'
            AND gg.imei = ts.imei)

结果为真。

| imei    |    date    | km    |
|------------------------------|
| 123     | 2019-01-15 | 15    |
| 456     | 2019-01-15 | 232   |

但是查询太复杂了。

编辑:表中有300万条记录。

1 个答案:

答案 0 :(得分:4)

您可以在子查询中找到每个imei-date对的第一个和最后一个日期时间,然后将其加入:

SELECT agg.imei, agg.date_date, gps_last.km - gps_frst.km AS diff
FROM (
    SELECT imei, DATE(date) AS date_date, MIN(date) AS date_frst, MAX(date) AS date_last
    FROM gps
    GROUP BY imei, DATE(date)
) AS agg
JOIN gps AS gps_frst ON agg.imei = gps_frst.imei AND agg.date_frst = gps_frst.date
JOIN gps AS gps_last ON agg.imei = gps_last.imei AND agg.date_last = gps_last.date

不过,您需要在表上使用适当的索引。特别是DATE(date)部分会很慢,因此您可能要考虑添加另一列以仅存储日期部分。