Oracle 11g-向数据透视表添加总计列

Oracle 11g-向数据透视表添加总计列,oracle,oracle11g,pivot-table,Oracle,Oracle11g,Pivot Table,我创建了一个透视表,其中包含来自多个表的数据(使用联接)。如何向表中添加另一列,将每行中的每一列相加 例如: Category | A | B | C | ABC 1 1 1 A 1 0 0 B 0 1 0 C 0 0 1 Category | A | B | C | TOTAL ABC 1 1

我创建了一个透视表,其中包含来自多个表的数据(使用联接)。如何向表中添加另一列,将每行中的每一列相加

例如:

Category  |  A  |  B  |  C |
ABC          1     1     1
A            1     0     0
B            0     1     0
C            0     0     1


Category  |  A  |  B  |  C | TOTAL
ABC          1     1     1    3
A            1     0     0    1
B            0     1     0    1 
C            0     0     1    1
如果您想添加一列,则可以添加一列,并使用以下步骤更新值:

alter table testing add total int;
使用以下步骤更新值

create or replace procedure add_Test
is
sqlis varchar2(10);
total1 int;
begin
for i in (select * from testing) loop
  select sum(a+b+c) into total1 from testing where category=i.category;
 update testing set total=total1 where category=i.category;
 end loop;
 commit;
 end;


exec add_test;

SCOTT@research 15-APR-15> select * from testing;

CATEG          A          B          C      TOTAL
----- ---------- ---------- ---------- ----------
ABC            1          1          1          3
A              1          0          0          1
B              0          1          0          1
C              0          0          1          1

您是说您编写了一个生成第一组结果的查询(可能是
select
中的
pivot
语句的结果)?或者您是说您有一个包含第一组数据的表(数据库中持久化的物理对象)?
create or replace procedure add_Test
is
sqlis varchar2(10);
total1 int;
begin
for i in (select * from testing) loop
  select sum(a+b+c) into total1 from testing where category=i.category;
 update testing set total=total1 where category=i.category;
 end loop;
 commit;
 end;


exec add_test;

SCOTT@research 15-APR-15> select * from testing;

CATEG          A          B          C      TOTAL
----- ---------- ---------- ---------- ----------
ABC            1          1          1          3
A              1          0          0          1
B              0          1          0          1
C              0          0          1          1