Skip to content
Advertisement

Set output to 1 if 5 or more days have passed since the last eligible entry

I have the below table:

I would like to implement this logic: the oldest line (S1) will have output 1. For the subsequent lines, if 5 or more days have passed from the previous entry with an output = 1, mark that output as 1 and continue evaluating.

The expected output would look something like this (4th column included for explanation purposes).

Advertisement

Answer

From Oracle 12, you can use:

Which, for the sample data:

Outputs:

ID DATE_CREATION OUTPUT
S1 01-JAN-22 1
S2 04-JAN-22 0
S3 07-JAN-22 1
S4 09-JAN-22 0
S5 10-JAN-22 0

db<>fiddle here

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement