通过放置总和(列)值更新记录

时间:2019-03-11 17:01:47

标签: sql oracle12c

我有这样的table1:

+-----+-----+------+
| cat | val | type |
+-----+-----+------+
| A   | 100 | c1   |
| H   | 25  | c2   |
| H   | 50  | c3   |
| H   | 30  | c2   |
| A   | 15  | c3   |
| H   | 10  | c1   |
| H   | 15  | c1   |
| B   | 10  | c4   |
| H   | 20  | c4   |
| H   | 15  | c3   |
+-----+-----+------+

我需要按类型将sum(val)组添加到每个类型的一个H中

所以按类型分组后,我得到了table2:

+-----+-----+
| cat | val |
+-----+-----+
| c1  | 125 |
| c2  | 55  |
| c3  | 80  |
| c4  | 30  |
+-----+-----+

我需要将125个添加到c1类型的任何H值中,将55添加到c2类型的任何H值中,依此类推。如果c1中没有H,则应该创建该记录。

所以最终我们得到了:

    +-----+-----+------+
    | cat | val | type |
    +-----+-----+------+
    | A   | 100 | c1   |
    | H   | 25  | c2   |
    | H   | 130 | c3   |
    | H   | 85  | c2   |
    | A   | 15  | c3   |
    | H   | 135 | c1   |
    | H   | 15  | c1   |
    | B   | 10  | c4   |
    | H   | 50  | c4   |
    | H   | 15  | c3   |
    +-----+-----+------+

如何不按类型对table1联合table2(以'H'作为cat)进行分组?另外,我没有更新权限,无法使用存储过程。我还必须记住,table1是涉及多个内部联接的查询的结果,我不想对select语句反复使用。

1 个答案:

答案 0 :(得分:0)

看看这是否有意义。我添加了ID列只是为了以与输入相同的顺序显示最终结果(以便于阅读)。

SQL> -- T1 is what you currently have; it can/could be your current query
SQL> with t1 (id, cat, val, type) as
  2    (select 1, 'A', 100, 'C1' from dual union all
  3     select 2, 'H', 25 , 'C2' from dual union all
  4     select 3, 'H', 50 , 'C3' from dual union all
  5     select 4, 'H', 30 , 'C2' from dual union all
  6     select 5, 'A', 15 , 'C3' from dual union all
  7     select 6, 'H', 10 , 'C1' from dual union all
  8     select 7, 'H', 15 , 'C1' from dual union all
  9     select 8, 'B', 10 , 'C4' from dual union all
 10     select 9, 'H', 20 , 'C4' from dual union all
 11     select 10,'H', 15 , 'C3' from dual
 12    ),
 13  -- sum VAL per type
 14  t1_sum as
 15    (select type, sum(val) sum_val
 16     from t1
 17     group by type
 18    ),
 19  -- find row number; let any H be number 1
 20  t1_rn as
 21    (select id, cat, val, type,
 22       row_number() over (partition by type
 23                          order by case when cat = 'H' then 1 end) rn
 24     from t1
 25    )
 26  -- the final result; add SUM_VAL to the first H row per type
 27  select r.cat, r.val + case when r.rn = 1 then s.sum_val else 0 end val,
 28    r.type
 29  From t1_rn r join t1_sum s on s.type = r.type
 30  order by r.id;

CAT        VAL TYPE
--- ---------- ----
A          100 C1
H           80 C2
H          130 C3
H           30 C2
A           15 C3
H          135 C1
H           15 C1
B           10 C4
H           50 C4
H           15 C3

10 rows selected.

SQL>

[编辑:尝试阐明如何使用大型查询]

假设这是您的非常大而复杂的查询:

select a.cat, 
       case when a.cat = 'A' then b.val 
            when a.cat = 'Z' then c.val
            else 'H'
       end val,
       c.type
from a join b on a.id = b.id and a.x = b.y
       join c on c.id = b.idx
where a.date_column < sysdate
  and c.type = 'X';

正如我所说的,基于它创建一个视图

create or replace view v_view as
select a.cat, 
       case when a.cat = 'A' then b.val 
            when a.cat = 'Z' then c.val
            else 'H'
       end val,
       c.type
from a join b on a.id = b.id and a.x = b.y
       join c on c.id = b.idx
where a.date_column < sysdate
  and c.type = 'X';

并将其用作“我的”查询的来源(从第14行开始):

with t1_sum as
  (select type, sum(val) sum_val
   from v_view                      --> here's the view
   group by type
  ), etc.

或者,将“巨大”查询本身用作初始CTE:

with t1 as
-- this is your "huge" query
(select a.cat,  
       case when a.cat = 'A' then b.val 
            when a.cat = 'Z' then c.val
            else 'H'
       end val,
       c.type
from a join b on a.id = b.id and a.x = b.y
       join c on c.id = b.idx
where a.date_column < sysdate
  and c.type = 'X'
),
-- sum VAL per type
t1_sum as
  (select type, sum(val) sum_val
   from t1
   group by type
  ), etc.