开发者

plsql- use an AVG,MAX function in UPDATE-SET

I want to update the value of a column that is grouped by using as criteria two other columns. assume table A (a Char, b integer, c interger,d) and the a column can include same object name, so i want to update the table with the condition: if column's a value and b value are found in two rows row1:(A1,2,numbery) and row2:(A1,2,numberx) then if: numberx,numbery>=0->AVG(third column), elsif numberx,numbery<0 THEN MIN else MAX (i.e -3,10->10).

--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


Create tables and sample data:

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.

To help figure out update, create a query that gives the desired results, as I understand them: (Not part of the solution, just part of solving)

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

Use merge to make available the agregate values during an update:

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

Rollback to initial test data:

SQL> rollback;

Rollback complete.

This query will not update rows where a change is not actually being made, reducing locking, undo and redo:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜