MySQL分组与时间差

时间:2018-09-17 05:00:09

标签: mysql sql database group-by

我的表记录如下:

id         startFrom                   endOn               gateway   broadcast
1    2018-09-01T23:41:24:024  2018-09-01T23:45:24:028         A         X01
2    2018-09-01T23:41:24:025  2018-09-01T23:45:24:032         A         X01
3    2018-09-01T23:41:24:026  2018-09-01T23:45:24:028         B         X01
4    2018-09-01T23:41:24:030  2018-09-01T23:45:24:045         C         X01
5    2018-09-01T22:41:24:029  2018-09-01T23:41:24:784         B         X02
6    2018-09-01T23:41:24:025  2018-09-01T23:45:24:038         A         X01

如果我想使用单个查询来查找特定广播中每个网关中的第一个请求时间段。如何使用MySQL对查询进行分组?

失败:

SELECT gateway, TIMESTAMPDIFF(MICROSECOND, `startFrom`, `endOn`) AS `timeDiff` FROM Records WHERE broadcast = 'X01' GROUP BY gateway

如果我尝试制作一个临时表,例如:

SELECT gateway, MIN(timeDiff)
FROM (SELECT TIMESTAMPDIFF(MICROSECOND, `startFrom`, `endOn`) AS `timeDiff`, gateway, broadcast FROM Records) t
WHERE broadcast = 'X01'
GROUP BY gateway;

在此广播中,它仅从每个网关的记录中获得最小时间差,但是我必须查询“第一条记录”(=> ORDER BY startFrom ASC)时间差。

目标输出为:

gateway        startFrom                 endOn                 timeDiff
A       2018-09-01T23:41:24:024  2018-09-01T23:45:24:028         4000  
B       2018-09-01T23:41:24:026  2018-09-01T23:45:24:028         2000  
C       2018-09-01T23:41:24:030  2018-09-01T23:45:24:045         15000  

感谢阅读和帮助

4 个答案:

答案 0 :(得分:0)

假设您想要每个网关的第一个并进行广播,请使用以下方法:

select gateway, broadcast, timeDiff from (
    SELECT gateway, broadcast, TIMESTAMPDIFF(MICROSECOND, startFrom, endOn) AS timeDiff,
        row_number() over (partition by gateway, broadcast order by startfrom) as rn
    FROM Records ) q
where rn=1

编辑: 查看mySQL版本8的文档,语法是不同的

select gateway, broadcast, timeDiff from (
    SELECT gateway, broadcast, TIMESTAMPDIFF(MICROSECOND, startFrom, endOn) AS timeDiff,
        row_number() over w as rn
    FROM Records
    WINDOW w as (partition by gateway, broadcast order by startfrom)
) q
where rn=1

答案 1 :(得分:0)

从记录中选择*;
+ ---- + --------------------- + --------------------- + --------- + ----------- +
| id | startFrom | endOn |网关|广播|
+ ---- + --------------------- + --------------------- + --------- + ----------- +
| 1 | 2018-09-17 05:23:53 | 2018-09-17 06:23:53 | A | X01 |
| 2 | 2018-09-17 05:24:08 | 2018-09-17 06:24:08 | A | X01 |
| 3 | 2018-09-17 05:24:25 | 2018-09-17 06:24:25 | B | X01 |
| 4 | 2018-09-17 05:24:32 | 2018-09-17 06:24:32 | C | X01 |
| 5 | 2018-09-17 05:24:42 | 2018-09-17 07:24:42 | B | X02 |
+ ---- + --------------------- + --------------------- + --------- + ----------- +

,这是您要查找的查询..

通过网关asc从记录组中选择网关,startFrom,endOn,time_to_sec(timediff(endOn,startFrom))作为timeDiff;
+ --------- + --------------------- + ----------------- ---- + ---------- +
|网关| startFrom | endOn | timeDiff |
+ --------- + --------------------- + ----------------- ---- + ---------- +
| A | 2018-09-17 05:23:53 | 2018-09-17 06:23:53 | 3600 |
| B | 2018-09-17 05:24:25 | 2018-09-17 06:24:25 | 3600 |
| C | 2018-09-17 05:24:32 | 2018-09-17 06:24:32 | 3600 |
+ --------- + --------------------- + ----------------- ---- + ---------- +

答案 2 :(得分:0)

尝试一下:

select gateway, min(startFrom),min(endOn), TIMESTAMPDIFF(MICROSECOND, min(startFrom), min(endOn)) AS `timeDiff` 
from records
where broadcast = 'X01' GROUP BY gateway

答案 3 :(得分:0)

您可以尝试以下类似方法,

DROP TABLE IF EXISTS groupTime ;
CREATE TABLE groupTime (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
startFrom DATETIME(3),
endOn DATETIME(3),
gateway CHAR(1),
broadcast VARCHAR(8)
);

INSERT INTO groupTime (startFrom,endOn,gateway ,broadcast)
VALUES
('2018-09-01 23:41:24.024','2018-09-01 23:45:24.028','A','X01'),
('2018-09-01 23:41:24.025','2018-09-01 23:45:24.032','A','X01'),
('2018-09-01 23:41:24.026','2018-09-01 23:45:24.028','B','X01'),
('2018-09-01 23:41:24.030','2018-09-01 23:45:24.045','C','X01'),
('2018-09-01 23:41:24.029','2018-09-01 23:41:24.784','B','X02'),
('2018-09-01 23:41:24.025','2018-09-01 23:45:24.038','A','X01');


SELECT a.gateway, a.startfrom, a.endOn , TIMESTAMPDIFF(MICROSECOND , a.startfrom, a.endOn) FROM groupTime a 
INNER JOIN (
    SELECT MIN(id) AS id FROM groupTime WHERE broadcast='X01' GROUP BY gateway 
)AS b
ON a.id = b.id

Demo