Select with aggregated function strange behavior
846417Mar 21 2011 — edited Mar 21 2011I created a simple query to reproduce this issue. The problem is when using SUM() between three tables, the values are not fetched as expected.
This is the simple script:
create table t1(clave number primary key,
descripcion varchar2(15));
create table t2(clave number primary key,
clave_t1 number,
total number,
CONSTRAINT t2_t1_fk
FOREIGN KEY (clave_t1)
REFERENCES t1(clave));
create table t3(clave number primary key,
clave_t1 number,
total number,
CONSTRAINT t3_t1_fk
FOREIGN KEY (clave_t1)
REFERENCES t1(clave));
insert into t1 values(1,'C7700');
insert into t2 values (1,1,1000000);
insert into t2 values (2,1,2000000);
insert into t2 values (3,1,5000000);
insert into t3 values (1,1,4000000);
insert into t3 values (2,1,5000000);
insert into t3 values (3,1,2000000);
insert into t3 values (4,1,3000000);
select sum(t2.total) t2total, sum(t3.total) t3total
from t1,t2,t3
where t2.clave_t1 = t1.clave
and t3.clave_t1 = t1.clave ;
Values fetched by the db engine are (32000000, 42000000) when they should be: (8000000,14000000). DB version is Oracle Express.
Thanks in advance for any help.