Oracle - 在查询结果之间插入新行

时间:2015-10-07 11:18:18

标签: sql oracle

我在这里遇到了一项棘手的任务。以下是示例数据:

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;

看起来如下:

input

我的目标是为每个事件创建

  • 一个新的前一条记录和

  • 一个新的成功记录。

需要在没有临时表的情况下完成(WITH AS就可以了)。

所需的输出如下所示:

desired output

箭头显示新列的数据取自何处。 规则:

  • 如果第一条记录包含DEPARTMENT_FROM ='UNASSIGNED',则新的前置记录将为DEPARTMENT_FROM ='1st-Level-Agent',否则为DEPARTMENT_FROM ='UNASSIGNED'。

但我觉得这些照片很清楚。

如何实现这一目标?

非常感谢您的进步!

2 个答案:

答案 0 :(得分:3)

表中的行没有任何隐式排序,如果您没有在SELECT语句中提供ORDER BY子句,数据库可以按照它想要的顺序自由返回行,因为您还没有被告知它如何订购返回的行。没有ORDER BY子句就没有"第一行"没有"最后一行"。如果你想要订购的东西你需要有一个字段(我经常有一个名为SORT_ORDER),它定义了如何排序行。因此,如果我已经有一个SORT_ORDER = 1的行,那么我可以添加"前面的"通过插入一个SORT_ORDER = 0的新行来进行排序。同样,我可以添加"以下"通过使用SORT_ORDER = 2插入新行来排序。但是在查询此表时,我需要记住指定ORDER BY SORT_ORDER

祝你好运。

答案 1 :(得分:0)

我试着给你一个可以适应你需求的例子。我使用分层查询来制定“预先”和“成功”。通过联合我加入了他们原始的数据集。我使用WITH来构建示例数据。希望有所帮助!

with 
   incidents as (
      select 1 as id, 'group 1' as "from", 'group 2' as "to" from dual
      union 
      select 1 as id, 'group 2' as "from", 'group 3' as "to" from dual
      union 
      select 2 as id, 'UNASSIGNED' as "from", 'G1' as "to" from dual
      )
select 0      as rwn, 
       id, 
       ( 
         case
           when "from" = 'UNASSIGNED' 
             then '1st-Level-Agent' 
             else 'UNASSIGNED'
         end
       )      as "from", 
       "from" as "to"
  from ( select t.*, level as lvl, connect_by_isleaf as leaf
           from incidents t 
        connect by prior "from" = "to" )
 where lvl = 1 
   and leaf = 1
union
select rownum as rwn, t2.* from incidents t2
union 
select 1000       as rwn, 
       id, 
       "to"       as "from", 
       'RESOLVED' as "to"
  from ( select t.*, level as lvl, connect_by_isleaf as leaf  
           from incidents t 
        connect by "from" = prior "to" )
 where lvl = 1 
   and leaf = 1
order by 2,1