2015-04-15 66 views
0

我创建了包含多个表(使用JOINS)的数据的数据透视表。我怎样才能添加另一列到表中,每行添加每列?Oracle 11g - 将总计列添加到数据透视表

例子:

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

你是说你写了一个查询来产生第一组结果(可能是因为'select'中的'pivot'语句)?或者你是否说第一组数据中有一张表(物理对象保存在数据库中)? –

回答

0
[email protected] 15-APR-15> select * from testing ; 

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

[email protected] 15-APR-15> select category,a,b,c, sum(a+b+c) as "total" from testing group by category,a,b,c order by category; 

CATEG   A   B   C  total 
----- ---------- ---------- ---------- ---------- 
A    1   0   0   1 
ABC   1   1   1   3 
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; 

[email protected] 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