2015-04-15 99 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