당신의 car trip data
테이블 trips
라는 가정 :
WITH starts_and_ends AS (
SELECT
starts.id,
starts.sequence AS start_sequence,
ends.sequence AS end_sequence
FROM
trips AS starts
JOIN trips AS ends
ON (starts.id = ends.id AND starts.sequence < ends.sequence)
WHERE
starts.latitude = 40.34 AND
starts.longitude = 23.50 AND
ends.latitude = 40.75 AND
ends.longitude = 23.90
)
SELECT
trips.*
FROM
starts_and_ends,
trips
WHERE
trips.id = starts_and_ends.id AND
sequence BETWEEN starts_and_ends.start_sequence AND starts_and_ends.end_sequence
ORDER BY
trips.id,
trips.sequence,
trips.timestamp;
쿼리 WITH
에서 나는 ID와 일련 번호를 시작 및 종료 지점을 선택합니다. 그런 다음 원본 테이블과 함께 여행을 보여줍니다.
출력 :
abc 2 2017-12-18 40.34 23.50
abc 3 2017-12-18 40.56 23.80
abc 4 2017-12-18 40.75 23.90
def 2 2017-12-15 40.34 23.50
def 3 2017-12-15 40.55 23.59
def 4 2017-12-15 40.80 23.99
def 5 2017-12-15 40.75 23.90