1
我需要能够做一个group by
但分段。所以在下面的例子中,有4个“组”代表一段旅程。SQL Server:GROUP BY多个相似/匹配行的段
SELECT id, mode, distance
FROM TravelLog;
输出:
-----------------------------------------------------------------------------
id mode distance
-----------------------------------------------------------------------------
1 on_foot 0.1
2 on_foot 0.2
3 on_foot 0.4
4 on_foot 0.6
5 on_foot 0.7
6 on_foot 0.8
7 in_vehicle 0.9
8 in_vehicle 2.0
9 in_vehicle 2.5
10 in_vehicle 3.0
11 in_vehicle 3.5
12 in_vehicle 4.0
13 in_vehicle 4.5
14 on_foot 4.6
15 on_foot 4.7
16 on_foot 4.8
17 on_foot 4.9
18 in_vehicle 5.5
19 in_vehicle 6.0
20 in_vehicle 6.5
-----------------------------------------------------------------------------
期望的结果:
-----------------------------------------------------------------------------
start_id end_id start_distance end_distance mode
-----------------------------------------------------------------------------
1 6 0.1 0.8 on_foot
7 13 0.9 4.5 in_vehicle
14 17 4.6 4.9 on_foot
18 20 5.5 6.5 in_vehicle
-----------------------------------------------------------------------------
这工作完美。而且,写这个问题花的时间比让你回答的时间要长!谢谢! – gunwin