如何使用 PSQL 将此 table 逐行分成两部分?
How do I split this table row by row into two using PSQL?
我有一个table (TABLE A) 它按时间记录出租车订单的每个起点和终点,现在我想找到第一个起点(即第2行),然后逐行配对 O 和 D 直到最后一个目的地(即第 9 行),结果应该像 TABLE B。我如何实现它?
TABLE一个
Time GPS_Point Status
633 POINT(121.314917 31.149205) Destination
62323 POINT(121.535798 31.25828) Origin
62328 POINT(121.535798 31.25828) Destination
62332 POINT(121.535798 31.25828) Origin
62429 POINT(121.5358 31.258278) Destination
62637 POINT(121.535788 31.25827) Origin
62647 POINT(121.535788 31.25827) Destination
62731 POINT(121.535795 31.25826) Origin
62741 POINT(121.535795 31.25826) Destination
62812 POINT(121.535793 31.25826) Origin
TABLE B
Origin_Time Origin_GPS_Point Destination_Time Destination_GPS_Point
62323 POINT(121.535798 31.25828) 62328 POINT(121.535798 31.25828)
62332 POINT(121.535798 31.25828) 62429 POINT(121.5358 31.258278)
62637 POINT(121.535788 31.25827) 62647 POINT(121.535788 31.25827)
62731 POINT(121.535795 31.25826) 62741 POINT(121.535795 31.25826)
使用这个查询:
select a.Time as 'Origin_Time', a.GPS_Point as 'Origin_GPS_Point', aa.Time as 'Destination_Time', aa.GPS_Point as 'Destination_GPS_Point'
from TABLE_A a
JOIN TABLE_A aa on a.GPS_Point=aa.GPS_Point and aa.Status='Destination' and a.Status='Origin'
你可以试试这个(但假设一些概念,如 Orig/Dest 的顺序没有任何中断)(我为 GPS_POINT 和 'O' 使用了一些假值作为原点和 'D' 作为目的地)。
CREATE TABLE TABLEA (TIME INT, GPS_POINT VARCHAR(10), STATUS VARCHAR(1));
INSERT INTO TABLEA VALUES (633,'p1','D');
INSERT INTO TABLEA VALUES (62323,'p2','O');
INSERT INTO TABLEA VALUES (62328,'p3','D');
INSERT INTO TABLEA VALUES (62332,'p4','O');
INSERT INTO TABLEA VALUES (62429,'p5','D');
INSERT INTO TABLEA VALUES (62637,'p6','O');
INSERT INTO TABLEA VALUES (62647,'p7','D');
INSERT INTO TABLEA VALUES (62650,'p8','O');
SELECT ORIGIN_TIME, ORIGIN_GPS, DEST_TIME, DEST_GPS FROM
(SELECT TIME AS ORIGIN_TIME, GPS_POINT AS ORIGIN_GPS, ROW_NUMBER() OVER (ORDER BY TIME) AS RN_O
FROM TABLEA
WHERE STATUS='O') A
LEFT JOIN (SELECT TIME AS DEST_TIME, GPS_POINT AS DEST_GPS, ROW_NUMBER() OVER (ORDER BY TIME) AS RN_D
FROM TABLEA
WHERE STATUS='D'
AND TIME> (SELECT MIN(TIME) FROM TABLEA)
) B ON A.RN_O = B.RN_D
WHERE DEST_TIME IS NOT NULL /* IF YOU WANT OMITS LAST "O" ROW WITHOUT "D" */
;
输出:
origin_time origin_gps dest_time dest_gps
1 62323 p2 62328 p3
2 62332 p4 62429 p5
3 62637 p6 62647 p7
我有一个table (TABLE A) 它按时间记录出租车订单的每个起点和终点,现在我想找到第一个起点(即第2行),然后逐行配对 O 和 D 直到最后一个目的地(即第 9 行),结果应该像 TABLE B。我如何实现它?
TABLE一个
Time GPS_Point Status
633 POINT(121.314917 31.149205) Destination
62323 POINT(121.535798 31.25828) Origin
62328 POINT(121.535798 31.25828) Destination
62332 POINT(121.535798 31.25828) Origin
62429 POINT(121.5358 31.258278) Destination
62637 POINT(121.535788 31.25827) Origin
62647 POINT(121.535788 31.25827) Destination
62731 POINT(121.535795 31.25826) Origin
62741 POINT(121.535795 31.25826) Destination
62812 POINT(121.535793 31.25826) Origin
TABLE B
Origin_Time Origin_GPS_Point Destination_Time Destination_GPS_Point
62323 POINT(121.535798 31.25828) 62328 POINT(121.535798 31.25828)
62332 POINT(121.535798 31.25828) 62429 POINT(121.5358 31.258278)
62637 POINT(121.535788 31.25827) 62647 POINT(121.535788 31.25827)
62731 POINT(121.535795 31.25826) 62741 POINT(121.535795 31.25826)
使用这个查询:
select a.Time as 'Origin_Time', a.GPS_Point as 'Origin_GPS_Point', aa.Time as 'Destination_Time', aa.GPS_Point as 'Destination_GPS_Point'
from TABLE_A a
JOIN TABLE_A aa on a.GPS_Point=aa.GPS_Point and aa.Status='Destination' and a.Status='Origin'
你可以试试这个(但假设一些概念,如 Orig/Dest 的顺序没有任何中断)(我为 GPS_POINT 和 'O' 使用了一些假值作为原点和 'D' 作为目的地)。
CREATE TABLE TABLEA (TIME INT, GPS_POINT VARCHAR(10), STATUS VARCHAR(1));
INSERT INTO TABLEA VALUES (633,'p1','D');
INSERT INTO TABLEA VALUES (62323,'p2','O');
INSERT INTO TABLEA VALUES (62328,'p3','D');
INSERT INTO TABLEA VALUES (62332,'p4','O');
INSERT INTO TABLEA VALUES (62429,'p5','D');
INSERT INTO TABLEA VALUES (62637,'p6','O');
INSERT INTO TABLEA VALUES (62647,'p7','D');
INSERT INTO TABLEA VALUES (62650,'p8','O');
SELECT ORIGIN_TIME, ORIGIN_GPS, DEST_TIME, DEST_GPS FROM
(SELECT TIME AS ORIGIN_TIME, GPS_POINT AS ORIGIN_GPS, ROW_NUMBER() OVER (ORDER BY TIME) AS RN_O
FROM TABLEA
WHERE STATUS='O') A
LEFT JOIN (SELECT TIME AS DEST_TIME, GPS_POINT AS DEST_GPS, ROW_NUMBER() OVER (ORDER BY TIME) AS RN_D
FROM TABLEA
WHERE STATUS='D'
AND TIME> (SELECT MIN(TIME) FROM TABLEA)
) B ON A.RN_O = B.RN_D
WHERE DEST_TIME IS NOT NULL /* IF YOU WANT OMITS LAST "O" ROW WITHOUT "D" */
;
输出:
origin_time origin_gps dest_time dest_gps
1 62323 p2 62328 p3
2 62332 p4 62429 p5
3 62637 p6 62647 p7