2011-02-08 62 views
0

我想更新通過使用兩個其他列作爲條件進行分組的列的值。假設表A(char,b integer,c interger,d)和一列可以包含相同的對象名稱,所以我想用條件更新表:如果在兩行中找到列的值和b值row1: (A1,2,numbery)和row2:(A1,2,numberx)then if:numberx,numbery> = 0-> AVG(第三列),elsif numberx,numbery < 0 THEN MIN else MAX(ie -3,10 - > 10)。plsql-在UPDATE-SET中使用AVG,MAX函數

--a--|--b--|--c--|--d-- desired TABLE update (A1,A2) --a--|--b--|--c--|--d-- 
A1  2  10 b          A1  2 "15" b  
A2  7  -9 bc         A2  7  "4" bc   
A3  7  12 fg         A3  7  12 fg 
A1  2  20 sa         A1  2 "15" sa 
A2  7  4 sa         A2  7 "4"  sa 
+1

至少請嘗試使其更具可讀性。 – 2011-02-08 12:20:41

回答

1

創建表和樣本數據:

SQL> create table t (a varchar2(20) not null 
    2  , b number(38) not null 
    3  , c number(38) not null 
    4  , d varchar2(20) not null); 

Table created. 

SQL> insert into t values ('A1', 2, 10, 'b'); 

1 row created. 

SQL> insert into t values ('A2', 7, -9, 'bc'); 

1 row created. 

SQL> insert into t values ('A3', 7, 12, 'fg'); 

1 row created. 

SQL> insert into t values ('A1', 2, 20, 'sa'); 

1 row created. 

SQL> insert into t values ('A2', 7, 4, 'sa'); 

1 row created. 

SQL> commit; 

Commit complete. 

爲了幫助找出更新,創建一個查詢,給出了預期的效果,我的理解是:(解決方案的不一部分,只是一部分問題)

SQL> select t.a, t.b 
    2  , case when agg_t.min_c >= 0 then avg_c -- all values of c are >= 0 
    3   when agg_t.max_c < 0 then min_c -- all values of c are < 0 
    4   else agg_t.max_c end as c 
    5  , t.d 
    6 from (select a, b, min(c) as min_c 
    7   , max(c) as max_c , avg(c) as avg_c 
    8  from t 
    9  group by a, b) agg_t 
10 inner join t on agg_t.a = t.a and agg_t.b = t.b 
11/

A        B   C D 
-------------------- ---------- ---------- -------------------- 
A3       7   12 fg 
A1       2   15 sa 
A2       7   4 bc 
A2       7   4 sa 
A1       2   15 b 

使用merge在更新過程中,使現有的agregate值:

SQL> merge into T dest 
    2 using (select a, b, min(c) as min_c 
    3   , max(c) as max_c , avg(c) as avg_c 
    4  from t 
    5  group by a, b) src 
    6 on (dest.a = src.a and dest.b = src.b) 
    7 when matched then update 
    8  set c = case when src.min_c >= 0 then avg_c -- all values of c are >= 0 
    9   when src.max_c < 0 then min_c -- all values of c are < 0 
10   else src.max_c end 
11/

5 rows merged. 

SQL> select * from t; 

A        B   C D 
-------------------- ---------- ---------- -------------------- 
A1       2   15 b 
A2       7   4 bc 
A3       7   12 fg 
A1       2   15 sa 
A2       7   4 sa 

回滾到最初的測試數據:

SQL> rollback; 

Rollback complete. 

這個查詢將不會更新,其中一個變化是不實際作出行,減少鎖定,撤消和重做:

SQL> merge into T dest 
    2 using (select a, b 
    3   , case when min_c >= 0 then avg_c -- all values of c are >= 0 
    4    when max_c < 0 then min_c -- all values of c are < 0 
    5    else max_c end as new_c 
    6  from (select a, b, min(c) as min_c 
    7    , max(c) as max_c , avg(c) as avg_c 
    8   from t 
    9   group by a, b)) src 
10 on (dest.a = src.a and dest.b = src.b) 
11 when matched then update set c = new_c 
12 where c <> new_c 
13 or (c is null and new_c is not null) 
14 or (c is not null and new_c is null) 
15/

3 rows merged. 

SQL> 
SQL> select * from t; 

A        B   C D 
-------------------- ---------- ---------- -------------------- 
A1       2   15 b 
A2       7   4 bc 
A3       7   12 fg 
A1       2   15 sa 
A2       7   4 sa