I've got a tricky task here. Here's the sample data:
create table incidents (
incident_id number,
region_id varchar2(100 char),
tasktype varchar2(100 char),
department_from varchar2(100 char),
department_to varchar2(100 char),
routing_date date,
failure_from date,
failure_to date,
incident_acception_date date,
incident_resolve_date date
);
Insert into incidents (incident_id,region_id,Tasktype,department_from,department_to,routing_date,failure_from,failure_to,incident_acception_date,incident_resolve_date) values (1,'RE2','Task','Group1','Group2',to_date('10.04.2015 23:54:21','dd.mm.yyyy hh24:mi:ss'),to_date('26.03.2015 22:00:00','dd.mm.yyyy hh24:mi:ss'),to_date('13.04.2015 18:25:00','dd.mm.yyyy hh24:mi:ss'),to_date('26.03.2015 22:32:00','dd.mm.yyyy hh24:mi:ss'),to_date('13.04.2015 18:50:07','dd.mm.yyyy hh24:mi:ss'));
Insert into incidents (incident_id,region_id,Tasktype,department_from,department_to,routing_date,failure_from,failure_to,incident_acception_date,incident_resolve_date) values (1,'RE2','Task','Group2','Group3',to_date('13.04.2015 07:19:05','dd.mm.yyyy hh24:mi:ss'),to_date('26.03.2015 22:00:00','dd.mm.yyyy hh24:mi:ss'),to_date('13.04.2015 18:25:00','dd.mm.yyyy hh24:mi:ss'),to_date('26.03.2015 22:32:00','dd.mm.yyyy hh24:mi:ss'),to_date('13.04.2015 18:50:07','dd.mm.yyyy hh24:mi:ss'));
Insert into incidents (incident_id,region_id,Tasktype,department_from,department_to,routing_date,failure_from,failure_to,incident_acception_date,incident_resolve_date) values (2,'RE2','Task','Group1','Group4',to_date('01.04.2015 21:26:16','dd.mm.yyyy hh24:mi:ss'),to_date('30.03.2015 13:00:00','dd.mm.yyyy hh24:mi:ss'),to_date('02.04.2015 09:30:00','dd.mm.yyyy hh24:mi:ss'),to_date('30.03.2015 14:09:00','dd.mm.yyyy hh24:mi:ss'),to_date('02.04.2015 10:06:13','dd.mm.yyyy hh24:mi:ss'));
Insert into incidents (incident_id,region_id,Tasktype,department_from,department_to,routing_date,failure_from,failure_to,incident_acception_date,incident_resolve_date) values (2,'RE2','Task','Group4','UNASSIGNED',to_date('01.04.2015 22:45:14','dd.mm.yyyy hh24:mi:ss'),to_date('30.03.2015 13:00:00','dd.mm.yyyy hh24:mi:ss'),to_date('02.04.2015 09:30:00','dd.mm.yyyy hh24:mi:ss'),to_date('30.03.2015 14:09:00','dd.mm.yyyy hh24:mi:ss'),to_date('02.04.2015 10:06:13','dd.mm.yyyy hh24:mi:ss'));
Insert into incidents (incident_id,region_id,Tasktype,department_from,department_to,routing_date,failure_from,failure_to,incident_acception_date,incident_resolve_date) values (2,'RE2','Task','UNASSIGNED','Group2',to_date('01.04.2015 22:45:32','dd.mm.yyyy hh24:mi:ss'),to_date('30.03.2015 13:00:00','dd.mm.yyyy hh24:mi:ss'),to_date('02.04.2015 09:30:00','dd.mm.yyyy hh24:mi:ss'),to_date('30.03.2015 14:09:00','dd.mm.yyyy hh24:mi:ss'),to_date('02.04.2015 10:06:13','dd.mm.yyyy hh24:mi:ss'));
Insert into incidents (incident_id,region_id,Tasktype,department_from,department_to,routing_date,failure_from,failure_to,incident_acception_date,incident_resolve_date) values (3,'RE2','Information','UNASSIGNED','Group1',to_date('01.04.2015 07:31:27','dd.mm.yyyy hh24:mi:ss'),to_date('01.04.2015 06:00:00','dd.mm.yyyy hh24:mi:ss'),to_date('11.06.2015 07:06:00','dd.mm.yyyy hh24:mi:ss'),to_date('01.04.2015 07:20:00','dd.mm.yyyy hh24:mi:ss'),to_date('16.06.2015 14:17:25','dd.mm.yyyy hh24:mi:ss'));
commit;
It looks as follows:
My aim is to create for each incident
one new preceding record and
one new succeeding record.
It needs to be done without a temporary table (WITH AS would be ok).
The desired output looks like this:
The arrows show where the data for the new column is taken from. Rules:
- If the first record contains DEPARTMENT_FROM='UNASSIGNED', the new preceeding record will be DEPARTMENT_FROM='1st-Level-Agent', else DEPARTMENT_FROM='UNASSIGNED'.
But I think the pictures makes it pretty clear.
How could this be achieved?
Thank you very much in advanced!


precedingandsucceeding. But you cannot guarantee that the retrieval of data will be in same order as it was inserted, unless you have a unique identifier and you order by it. So if you insert 5 names in db, and select them again, you might not get them in the same order as they were inserted. Hence there is noprecedingandsucceedingin relational db. To achieve what you want, you have to first add a key and order by it. Then you can have anyprecedingandsucceedingrows