Hi,
I have a dataset which is part of a much larger query and need help in selecting certain rows based on the value of a column: processed_flag.
Here are the general guidelines: If in a dataset none of the rows have been processed then pick the row with the earliest start and end date. If a particular row has already been processed which is denoted by the flag: processed_flag = 'Y' then pick the very next row to process. If the very last row in the data set has a processed_flag = 'Y' then pick the same very last row to be processed again.
Here is an example: (The dataset will be ordered by start_date ASC, end_date ASC and the value of the processed_flag determines which row needs to be serviced next.)
Scenario 1: If any of the rows in the current dataset in question has a value of "Y" then pick the very next row to be processed. In this case (From the screenshot above) record_number = 3 is the answer.
Scenario 2: If in the dataset, the very last row has a value of "Y" then pick the same row to be processed again. In this case (From the screenshot above) record_number = 4 is the answer.
Scenario 3: If there are no "Y" values for the column processed_flag in a particular dataset then pick the very first row from the dataset to be processed. In this case (From the screenshot above) record_number = 1 is the answer.
I am hoping to use analytic function to resolve this and may be have an additional column at the end saying "next_record_to_be_processed" which will hold the answer for a given subset of data or please advise if there is a better way to accomplish the same.
Any help is appreciated. Thanks a lot!
--DDL and DMLs below
CREATE TABLE temp
(
record_number NUMBER
, start_date DATE
, end_date DATE
, processed_flag VARCHAR2(1)
);
--Scenario 1
INSERT INTO temp VALUES (1, TO_DATE('06/01/2022','MM/DD/YYYY'), TO_DATE('06/30/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (2, TO_DATE('07/01/2022','MM/DD/YYYY'), TO_DATE('07/15/2022','MM/DD/YYYY'), 'Y');
INSERT INTO temp VALUES (3, TO_DATE('07/16/2022','MM/DD/YYYY'), TO_DATE('08/15/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (4, TO_DATE('08/16/2022','MM/DD/YYYY'), NULL, 'N');
--TRUNCATE TABLE temp;
--Scenario 2
INSERT INTO temp VALUES (1, TO_DATE('06/01/2022','MM/DD/YYYY'), TO_DATE('06/30/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (2, TO_DATE('07/01/2022','MM/DD/YYYY'), TO_DATE('07/15/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (3, TO_DATE('07/16/2022','MM/DD/YYYY'), TO_DATE('08/15/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (4, TO_DATE('08/16/2022','MM/DD/YYYY'), NULL, 'Y');
--TRUNCATE TABLE temp;
--Scenario 3
INSERT INTO temp VALUES (1, TO_DATE('06/01/2022','MM/DD/YYYY'), TO_DATE('06/30/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (2, TO_DATE('07/01/2022','MM/DD/YYYY'), TO_DATE('07/15/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (3, TO_DATE('07/16/2022','MM/DD/YYYY'), TO_DATE('08/15/2022','MM/DD/YYYY'), 'N');
INSERT INTO temp VALUES (4, TO_DATE('08/16/2022','MM/DD/YYYY'), NULL, 'N');