提问者:小点点

Mysql视图:批处理中的第一个和最后一个


我有一个数据集,如下例所示。

id,start_location,start_time,end_location,end_time
1P18UA,A,15/06/2020 05:48,B,15/06/2020 05:58
1P18UA,B,15/06/2020 05:59,C,15/06/2020 06:30
1P18UA,C,15/06/2020 06:32,D,15/06/2020 06:55
1P18UA,D,15/06/2020 06:57,E,15/06/2020 07:20

我希望创建一个视图,该视图显示每个记录补丁的单行,例如

id,start_location,start_time,end_location,end_time
1P18UA,A,15/06/2020 05:48,E,15/06/2020 07:20

即显示批处理中第一行的开始时间和位置以及同一批处理中最后一行的结束位置和时间。


共1个答案

匿名用户

您可以尝试下面的查询。

  1. 获取每个ID的最小start_time和最大end_time.
  2. 连接到主表以获取start_location和END_LOCATION
select a.id, a.start_location, b.start_time, a.end_location, b.end_time
from tbl a join (
  select id, min(start_time) start_time, max(end_time) end_time
  from tbl group by id
) b on a.id = b.id 
and a.start_time = b.start_time
and a.end_time = b.end_time