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