2015-10-07 90 views
0
之间插入新行

我在这里有一个棘手的任务。这里的样本数据: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 =“未分配”,新前述记录将DEPARTMENT_FROM =“1级代理”,否则DEPARTMENT_FROM =“未分配”。

但我认为这些照片使它很清楚。

这怎么可能实现?

非常感谢您的高级!

+8

“之前”和“后继”在关系数据库中没有意义。你需要更仔细地考虑你的要求。 – duffymo

+0

我想在第一个现有行之前创建一个新行,并且在这种情况下每个事件都创建一个新的行,它成功了最后一个现有行。为什么它没有意义?我想以这种方式“丰富”现有的数据以便进一步分析。 – royskatt

+0

我明白你的意思是“前置”和“后继”。但是,您不能保证数据检索的顺序与插入数据的顺序相同,除非您有唯一的标识符并按顺序排序。所以如果你在db中插入5个名字,并再次选择它们,你可能无法按照它们插入的顺序得到它们。因此在关系数据库中没有'前面的'和'后面的'。为了实现你想要的,你必须先添加一个密钥并按顺序排列。然后你可以有任何'前面的'和'后面的行 – Utsav

回答

3

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

好运。

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