CREATE TABLE tst_tbl
(
id NUMBER,
last_name VARCHAR2 (50),
first_name VARCHAR2 (50),
dob DATE,
register_dt DATE,
register_loc VARCHAR2 (50),
visit_dt DATE,
visit_loc VARCHAR2 (50),
visit_comments VARCHAR2 (30)
);
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('09/05/2017' ,'MM/DD/YYYY') ,'NEW YORK', to_date('02/26/2018','MM/DD/YYYY'), 'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('09/05/2017' ,'MM/DD/YYYY') ,'NEW YORK', to_date('2/12/2018', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('09/05/2017' ,'MM/DD/YYYY') ,'NEW YORK', to_date('11/6/2017', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('09/05/2017' ,'MM/DD/YYYY') ,'NEW YORK', to_date('10/23/2017','MM/DD/YYYY'), 'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('09/05/2017' ,'MM/DD/YYYY') ,'NEW YORK', to_date('3/27/2018', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('09/05/2017' ,'MM/DD/YYYY') ,'NEW YORK', to_date('3/19/2018', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('09/05/2017' ,'MM/DD/YYYY') ,'NEW YORK', to_date('9/11/2017', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('2/7/2018' ,'MM/DD/YYYY') , 'NEW YORK',to_date('11/6/2017 ','MM/DD/YYYY'), 'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('2/7/2018' ,'MM/DD/YYYY') , 'NEW YORK',to_date('3/19/2018', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('2/7/2018' ,'MM/DD/YYYY') , 'NEW YORK',to_date('9/11/2017', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('2/7/2018' ,'MM/DD/YYYY') , 'NEW YORK',to_date('3/27/2018', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('2/7/2018' ,'MM/DD/YYYY') , 'NEW YORK',to_date('2/26/2018', 'MM/DD/YYYY'),'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('2/7/2018' ,'MM/DD/YYYY') , 'NEW YORK',to_date('10/23/2017','MM/DD/YYYY'), 'NEW JERSEY', '');
INSERT INTO tst_tbl VALUES(1234, 'John', 'Smith', to_date('12/01/1980','MM/DD/YYYY') , to_date('2/7/2018' ,'MM/DD/YYYY') , 'NEW YORK',to_date('2/12/2018', 'MM/DD/YYYY'),'NEW JERSEY', '');
COMMIT;
Я хочу получить информацию о немедленном посещении (visit_dt, visit_loc, ..) после даты регистрации.
например:
1234, John, Smith, 12/01/1980, 09/05/2017, NEW YORK, 9/11/2017, NEW JERSEY
1234, John, Smith, 12/01/1980, 2/7/2018, NEW YORK, 2/12/2018, NEW JERSEY
Я попытался с помощью приведенной ниже логики отсортировать даты регистрации и даты посещения, а затем использовать свинец для получения следующей даты и фильтрации только для дат регистрации. Но я не могу добавить другие поля, как показано выше ..
SELECT
dt, vst_type, register_dt, vst_dt
FROM
(
SELECT
id, dt, vst_type,
dt AS register_dt,
ROW_NUMBER () OVER
(
PARTITION BY id, dt ORDER BY
CASE
WHEN vst_type = 'REGISTER_DT' THEN 1 ELSE 2 END
)
AS vst_dt_rnum,
LEAD(dt) OVER (PARTITION BY id ORDER BY dt) AS vst_dt
FROM
(
SELECT id, register_dt AS dt, 'REGISTER_DT' vst_type FROM tst_tbl
UNION
SELECT id, visit_dt AS dt, 'VISIT_DT' vst_type FROM tst_tbl
)
)
WHERE vst_dt_rnum = 1 AND vst_type = 'REGISTER_DT'