2015-10-07 133 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