Untitled

 avatar
unknown
plain_text
3 months ago
445 B
7
Indexable
WITH ranked_trips AS (
    SELECT
        vehicle_id,
        trip_id,
        start_time,
        stop_time,
        LAG(stop_time) OVER (PARTITION BY vehicle_id ORDER BY start_time) AS previous_stop_time,
        ROW_NUMBER() OVER (PARTITION BY vehicle_id ORDER BY start_time DESC) AS rn
    FROM
        trips
)
SELECT
    vehicle_id,
    trip_id,
    start_time,
    stop_time,
    previous_stop_time
FROM
    ranked_trips
WHERE
    rn = 1;
Editor is loading...
Leave a Comment