You Asked
Hello everyone!
I have a puzzle that has stumped me. I have played with variations of nested selects, hierarchical queries, analytic functions, and have gotten close to what I need, but not all the way there.
I am sure that I am missing something easy here.
The data organized by BLOCKID. Inside that BLOCKID it is sorted by SCHED_DEP_TIME and then by ID. There are other fiels, but these are all that is needed for this question.
I would like to get (sample is below) is the same data back with the next ID and its time in the same BLOCKID by time.
Thanks for any help!
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9434281',to_timestamp('09-FEB-16 03.58.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9434281',to_timestamp('09-FEB-16 04.09.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421120',to_timestamp('09-FEB-16 04.10.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421120',to_timestamp('09-FEB-16 04.17.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421120',to_timestamp('09-FEB-16 04.40.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421120',to_timestamp('09-FEB-16 04.45.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421120',to_timestamp('09-FEB-16 04.53.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421223',to_timestamp('09-FEB-16 05.10.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421223',to_timestamp('09-FEB-16 05.18.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421223',to_timestamp('09-FEB-16 05.50.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421223',to_timestamp('09-FEB-16 05.57.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421223',to_timestamp('09-FEB-16 06.08.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421073',to_timestamp('09-FEB-16 06.27.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421073',to_timestamp('09-FEB-16 06.39.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421073',to_timestamp('09-FEB-16 06.47.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421073',to_timestamp('09-FEB-16 07.05.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421073',to_timestamp('09-FEB-16 07.13.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421073',to_timestamp('09-FEB-16 07.27.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421224',to_timestamp('09-FEB-16 07.48.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421224',to_timestamp('09-FEB-16 07.57.00.000000000 AM'),'001HL001');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421933',to_timestamp('09-FEB-16 08.24.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421933',to_timestamp('09-FEB-16 08.32.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421933',to_timestamp('09-FEB-16 08.42.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421933',to_timestamp('09-FEB-16 09.10.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9422613',to_timestamp('09-FEB-16 09.10.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9422613',to_timestamp('09-FEB-16 09.12.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421132',to_timestamp('09-FEB-16 09.28.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421132',to_timestamp('09-FEB-16 09.29.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421132',to_timestamp('09-FEB-16 09.54.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421132',to_timestamp('09-FEB-16 09.56.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421952',to_timestamp('09-FEB-16 10.23.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421952',to_timestamp('09-FEB-16 10.26.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9422609',to_timestamp('09-FEB-16 10.34.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421126',to_timestamp('09-FEB-16 10.48.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421126',to_timestamp('09-FEB-16 10.49.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421126',to_timestamp('09-FEB-16 11.20.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421126',to_timestamp('09-FEB-16 11.26.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421126',to_timestamp('09-FEB-16 11.38.00.000000000 AM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421958',to_timestamp('09-FEB-16 12.10.00.000000000 PM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421958',to_timestamp('09-FEB-16 12.20.00.000000000 PM'),'001HL002');
Insert into EXPORT_TABLE (ID,SCHED_DEP_TIME,BLOCKID) values ('9421958',to_timestamp('09-FEB-16 12.26.00.000000000 PM'),'001HL002');
The desired output is as follows:
ID, SCHED_DEP_TIME, BLOCKID,NEXTID,NEXTIDTIME
where NEXTID is the next distinct ID from withing the same blockID, ordered by SCHED_DEP_TIME
and NEXTIDTIME is that NEXTID's SCHED_DEP_TIME
like this:
9434281,09-FEB-16 03.58.00.000000000 AM,001HL001,9421120,09-FEB-16 04.10.00.000000000 AM
9434281,09-FEB-16 04.09.00.000000000 AM,001HL001,9421120,09-FEB-16 04.10.00.000000000 AM
9421120,09-FEB-16 04.10.00.000000000 AM,001HL001,9421223,09-FEB-16 05.10.00.000000000 AM
9421120,09-FEB-16 04.17.00.000000000 AM,001HL001,9421223,09-FEB-16 05.10.00.000000000 AM
9421120,09-FEB-16 04.40.00.000000000 AM,001HL001,9421223,09-FEB-16 05.10.00.000000000 AM
9421120,09-FEB-16 04.45.00.000000000 AM,001HL001,9421223,09-FEB-16 05.10.00.000000000 AM
9421120,09-FEB-16 04.53.00.000000000 AM,001HL001,9421223,09-FEB-16 05.10.00.000000000 AM
9421223,09-FEB-16 05.10.00.000000000 AM,001HL001,9421073,09-FEB-16 06.27.00.000000000 AM
9421223,09-FEB-16 05.18.00.000000000 AM,001HL001,9421073,09-FEB-16 06.27.00.000000000 AM
9421223,09-FEB-16 05.50.00.000000000 AM,001HL001,9421073,09-FEB-16 06.27.00.000000000 AM
9421223,09-FEB-16 05.57.00.000000000 AM,001HL001,9421073,09-FEB-16 06.27.00.000000000 AM
9421223,09-FEB-16 06.08.00.000000000 AM,001HL001,9421073,09-FEB-16 06.27.00.000000000 AM
9421073,09-FEB-16 06.27.00.000000000 AM,001HL001,9421224,09-FEB-16 07.48.00.000000000 AM
9421073,09-FEB-16 06.39.00.000000000 AM,001HL001,9421224,09-FEB-16 07.48.00.000000000 AM
9421073,09-FEB-16 06.47.00.000000000 AM,001HL001,9421224,09-FEB-16 07.48.00.000000000 AM
9421073,09-FEB-16 07.05.00.000000000 AM,001HL001,9421224,09-FEB-16 07.48.00.000000000 AM
9421073,09-FEB-16 07.13.00.000000000 AM,001HL001,9421224,09-FEB-16 07.48.00.000000000 AM
9421073,09-FEB-16 07.27.00.000000000 AM,001HL001,9421224,09-FEB-16 07.48.00.000000000 AM
9421224,09-FEB-16 07.48.00.000000000 AM,001HL001,null,null
9421224,09-FEB-16 07.57.00.000000000 AM,001HL001,null,null
9421933,09-FEB-16 08.24.00.000000000 AM,001HL002,9422613,09-FEB-16 09.10.00.000000000 AM
9421933,09-FEB-16 08.32.00.000000000 AM,001HL002,9422613,09-FEB-16 09.10.00.000000000 AM
9421933,09-FEB-16 08.42.00.000000000 AM,001HL002,9422613,09-FEB-16 09.10.00.000000000 AM
9421933,09-FEB-16 09.10.00.000000000 AM,001HL002,9422613,09-FEB-16 09.10.00.000000000 AM
9422613,09-FEB-16 09.10.00.000000000 AM,001HL002,9421132,09-FEB-16 09.28.00.000000000 AM
9422613,09-FEB-16 09.12.00.000000000 AM,001HL002,9421132,09-FEB-16 09.28.00.000000000 AM
9421132,09-FEB-16 09.28.00.000000000 AM,001HL002,9421952,09-FEB-16 10.23.00.000000000 AM
9421132,09-FEB-16 09.29.00.000000000 AM,001HL002,9421952,09-FEB-16 10.23.00.000000000 AM
9421132,09-FEB-16 09.54.00.000000000 AM,001HL002,9421952,09-FEB-16 10.23.00.000000000 AM
9421132,09-FEB-16 09.56.00.000000000 AM,001HL002,9421952,09-FEB-16 10.23.00.000000000 AM
9421952,09-FEB-16 10.23.00.000000000 AM,001HL002,9422609,09-FEB-16 10.34.00.000000000 AM
9421952,09-FEB-16 10.26.00.000000000 AM,001HL002,9422609,09-FEB-16 10.34.00.000000000 AM
9422609,09-FEB-16 10.34.00.000000000 AM,001HL002,9421126,09-FEB-16 10.48.00.000000000 AM
9421126,09-FEB-16 10.48.00.000000000 AM,001HL002,9421958,09-FEB-16 12.10.00.000000000 PM
9421126,09-FEB-16 10.49.00.000000000 AM,001HL002,9421958,09-FEB-16 12.10.00.000000000 PM
9421126,09-FEB-16 11.20.00.000000000 AM,001HL002,9421958,09-FEB-16 12.10.00.000000000 PM
9421126,09-FEB-16 11.26.00.000000000 AM,001HL002,9421958,09-FEB-16 12.10.00.000000000 PM
9421126,09-FEB-16 11.38.00.000000000 AM,001HL002,9421958,09-FEB-16 12.10.00.000000000 PM
9421958,09-FEB-16 12.10.00.000000000 PM,001HL002,null,null
9421958,09-FEB-16 12.20.00.000000000 PM,001HL002,null,null
9421958,09-FEB-16 12.26.00.000000000 PM,001HL002,null,null
and Connor said...
Is this what you're after ?
SQL> with t as (
2 select
3 ID,
4 min(SCHED_DEP_TIME) min_sched,
5 max(SCHED_DEP_TIME) max_sched,
6 BLOCKID
7 from export_table
8 group by id,blockid
9 ),
10 next_Details as (
11 select ID,
12 BLOCKID,
13 min_sched,
14 max_sched,
15 lead(id) over (partition by blockid order by min_sched ) as next_id,
16 lead(min_sched) over ( partition by blockid order by min_sched ) as next_sched
17 from t
18 )
19 select e.ID,
20 e.BLOCKID,
21 e.SCHED_DEP_TIME,
22 n.next_id,
23 n.next_sched
24 from next_Details n,
25 export_table e
26 where n.id = e.id
27 and n.blockid = e.blockid
28 and e.SCHED_DEP_TIME between n.min_sched and n.max_sched
29 order by BLOCKID, SCHED_DEP_TIME;
ID BLOCKID SCHED_DEP_TIME NEXT_ID NEXT_SCHED
-------------------- -------------------- ---------------------------------------- -------------------- --------------------------
----
9434281 001HL001 09-FEB-16 03.58.00.000000 AM 9421120 09-FEB-16 04.10.00.000000 AM
9434281 001HL001 09-FEB-16 04.09.00.000000 AM 9421120 09-FEB-16 04.10.00.000000 AM
9421120 001HL001 09-FEB-16 04.10.00.000000 AM 9421223 09-FEB-16 05.10.00.000000 AM
9421120 001HL001 09-FEB-16 04.17.00.000000 AM 9421223 09-FEB-16 05.10.00.000000 AM
9421120 001HL001 09-FEB-16 04.40.00.000000 AM 9421223 09-FEB-16 05.10.00.000000 AM
9421120 001HL001 09-FEB-16 04.45.00.000000 AM 9421223 09-FEB-16 05.10.00.000000 AM
9421120 001HL001 09-FEB-16 04.53.00.000000 AM 9421223 09-FEB-16 05.10.00.000000 AM
9421223 001HL001 09-FEB-16 05.10.00.000000 AM 9421073 09-FEB-16 06.27.00.000000 AM
9421223 001HL001 09-FEB-16 05.18.00.000000 AM 9421073 09-FEB-16 06.27.00.000000 AM
9421223 001HL001 09-FEB-16 05.50.00.000000 AM 9421073 09-FEB-16 06.27.00.000000 AM
9421223 001HL001 09-FEB-16 05.57.00.000000 AM 9421073 09-FEB-16 06.27.00.000000 AM
9421223 001HL001 09-FEB-16 06.08.00.000000 AM 9421073 09-FEB-16 06.27.00.000000 AM
9421073 001HL001 09-FEB-16 06.27.00.000000 AM 9421224 09-FEB-16 07.48.00.000000 AM
9421073 001HL001 09-FEB-16 06.39.00.000000 AM 9421224 09-FEB-16 07.48.00.000000 AM
9421073 001HL001 09-FEB-16 06.47.00.000000 AM 9421224 09-FEB-16 07.48.00.000000 AM
9421073 001HL001 09-FEB-16 07.05.00.000000 AM 9421224 09-FEB-16 07.48.00.000000 AM
9421073 001HL001 09-FEB-16 07.13.00.000000 AM 9421224 09-FEB-16 07.48.00.000000 AM
9421073 001HL001 09-FEB-16 07.27.00.000000 AM 9421224 09-FEB-16 07.48.00.000000 AM
9421224 001HL001 09-FEB-16 07.48.00.000000 AM
9421224 001HL001 09-FEB-16 07.57.00.000000 AM
9421933 001HL002 09-FEB-16 08.24.00.000000 AM 9422613 09-FEB-16 09.10.00.000000 AM
9421933 001HL002 09-FEB-16 08.32.00.000000 AM 9422613 09-FEB-16 09.10.00.000000 AM
9421933 001HL002 09-FEB-16 08.42.00.000000 AM 9422613 09-FEB-16 09.10.00.000000 AM
9421933 001HL002 09-FEB-16 09.10.00.000000 AM 9422613 09-FEB-16 09.10.00.000000 AM
9422613 001HL002 09-FEB-16 09.10.00.000000 AM 9421132 09-FEB-16 09.28.00.000000 AM
9422613 001HL002 09-FEB-16 09.12.00.000000 AM 9421132 09-FEB-16 09.28.00.000000 AM
9421132 001HL002 09-FEB-16 09.28.00.000000 AM 9421952 09-FEB-16 10.23.00.000000 AM
9421132 001HL002 09-FEB-16 09.29.00.000000 AM 9421952 09-FEB-16 10.23.00.000000 AM
9421132 001HL002 09-FEB-16 09.54.00.000000 AM 9421952 09-FEB-16 10.23.00.000000 AM
9421132 001HL002 09-FEB-16 09.56.00.000000 AM 9421952 09-FEB-16 10.23.00.000000 AM
9421952 001HL002 09-FEB-16 10.23.00.000000 AM 9422609 09-FEB-16 10.34.00.000000 AM
9421952 001HL002 09-FEB-16 10.26.00.000000 AM 9422609 09-FEB-16 10.34.00.000000 AM
9422609 001HL002 09-FEB-16 10.34.00.000000 AM 9421126 09-FEB-16 10.48.00.000000 AM
9421126 001HL002 09-FEB-16 10.48.00.000000 AM 9421958 09-FEB-16 12.10.00.000000 PM
9421126 001HL002 09-FEB-16 10.49.00.000000 AM 9421958 09-FEB-16 12.10.00.000000 PM
9421126 001HL002 09-FEB-16 11.20.00.000000 AM 9421958 09-FEB-16 12.10.00.000000 PM
9421126 001HL002 09-FEB-16 11.26.00.000000 AM 9421958 09-FEB-16 12.10.00.000000 PM
9421126 001HL002 09-FEB-16 11.38.00.000000 AM 9421958 09-FEB-16 12.10.00.000000 PM
9421958 001HL002 09-FEB-16 12.10.00.000000 PM
9421958 001HL002 09-FEB-16 12.20.00.000000 PM
9421958 001HL002 09-FEB-16 12.26.00.000000 PM
41 rows selected.
I collapsed the data into 1 row per ID, so I can use lead to get the logical "next" ID, and the join back to get the original rows.
Rating
(6 ratings)
We're not taking comments currently, so please try again later if you want to add a comment.