mysql按时间差分组

jchrr9hc  于 2021-06-21  发布在  Mysql
关注(0)|答案(22)|浏览(737)

我的表格记录如下:

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

谢谢你的阅读和帮助

pgvzfuti

pgvzfuti17#

你可以试试下面的方法,

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

演示

anhgbhbe

anhgbhbe19#

试试这个:

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

lskq00tm22#

假设每个网关和广播都需要第一个,请使用以下命令:

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 doc,语法是不同的

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

相关问题