如何在Cloud Spanner中加入左下一行。
我想计算每个司机行驶了多少公里。
我的表如下所示:
vehicle_id | driver_id | odometer
1 | 1 | 10
1 | 1 | 20
1 | 2 | 20
1 | 2 | 40
1 | 1 | 40
1 | 1 | 50
2 | 1 | 10
2 | 1 | 20
2 | 2 | 20
2 | 2 | 30
2 | 1 | 30
2 | 1 | 80
2 | 2 | 80
2 | 2 | 120
结果应为:
driver_id | total_mileage
1 | 80
2 | 70
我的解决方案是:
SUM (mileage)
FROM (SELECT (odometer2-odometer) AS mileage
FROM (SELECT vehicle_id , odometer ,driver_id ,
NEXT.driver_id AS driver_id 2, NEXT.odometer AS odometer2 FROM Table
**JOIN NEXT ROW** AS NEXT
)
WHERE driver_id=driver_id2
)
GROUP BY driver_id
vehicle_id | driver_id | odometer | driver_id2 | odometer2 |mileage
1 | 1 | 10 | 1 | 20 | 10
1 | 1 | 20 | 2 | 20 | -
1 | 2 | 20 | 2 | 40 | 20
1 | 2 | 40 | 1 | 40 | -
1 | 1 | 40 | 1 | 50 | 10
1 | 1 | 50 | - | - | -
2 | 1 | 10 | 1 | 20 | 10
2 | 1 | 20 | 2 | 20 | -
2 | 2 | 20 | 2 | 30 | 10
2 | 2 | 30 | 1 | 30 | -
2 | 1 | 30 | 1 | 80 | 50
2 | 1 | 80 | 2 | 80 | -
2 | 2 | 80 | 2 | 120 | 40
2 | 2 | 120 | - | - | -
在云扳手功能Row_number中,OVER,LAG不存在。我的问题是如何在云扳手中加入左下一行?
您希望透视数据。 这不是直接在Cloud Spanner SQL中可行的 - 您需要对数据进行后处理以影响透视。