I have a table called as Activity with columns like activity_id and activity_date.
Consider a data in activity table like below,
activity_id activity_date
1 1st June
2 1st July
3 1st August
4 1st September
5 1st October
Now I want to change the date of the activity 3, but I can not change the date to less than 1st July or more than 1st September as there are already some other activities on those dates.
The only valid dates for activity 3 are between 2nd July to 30th August.
Similarly, for activity 1, valid new date can be any date before 1st July.
Similarly, for activity 5, valid new date ranges from 2nd September to any date in future as its last activity.
I need to give the validation message to the user in front end if the new date is not within the range.
Input to the query will be activity id and the new activity date.
Below is the DDL script
CREATE TABLE "HEADCOUNT"."ACTIVITY"
( "ACTIVITY_ID" NUMBER(*,0) NOT NULL,
"ACTIVITY_DATE" DATE
);
Insert into "HEADCOUNT"."ACTIVITY" (ACTIVITY_ID,ACTIVITY_DATE) values (1,'01-06-2012');
Insert into "HEADCOUNT"."ACTIVITY" (ACTIVITY_ID,ACTIVITY_DATE) values (2,'01-07-2012');
Insert into "HEADCOUNT"."ACTIVITY" (ACTIVITY_ID,ACTIVITY_DATE) values (3,'01-08-2012');
Insert into "HEADCOUNT"."ACTIVITY" (ACTIVITY_ID,ACTIVITY_DATE) values (4,'01-09-2012');
Insert into "HEADCOUNT"."ACTIVITY" (ACTIVITY_ID,ACTIVITY_DATE) values (5,'01-10-2012');
This will find the date ranges for each row:
Result:
Validation would then be for a given id:
“input date” > previous_date AND “input date” < next_date
Date ranges are based on previous and following records when ordered by activity_id. Perhaps the ordering should really be by activity_date, though. Using LAG and LEAD will allow for gaps in activity_ids as well.