Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help with analytic function

User_K6LH0Jun 22 2022

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.)
Screen Shot 2022-06-21 at 11.02.59 PM.pngScenario 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.
Screen Shot 2022-06-21 at 11.25.35 PM.pngScenario 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.
Screen Shot 2022-06-21 at 11.27.43 PM.pngScenario 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');


This post has been answered by Solomon Yakobson on Jun 22 2022
Jump to Answer

Comments

Post Details

Added on Jun 22 2022
12 comments
201 views