现在要 求 Q 表中 a,b 和 与 P 表 d,e 和 相加 的值
select sum(Q.a+Q.b) + sum(P.d+P.e) as total from P ,Q
为什么算出来 的 值不对
20 个解决方案
#1
Q与P的关联条件是什么?
#2
如果仅是想把它们加总,可以这样:
Select Sum(Ttl) From(
Select a Ttl From Q
Union All
Select b Ttl From Q
Union All
Select d Ttl From P
Union All
Select e Ttl From P
)
Select Sum(Ttl) From(
Select a Ttl From Q
Union All
Select b Ttl From Q
Union All
Select d Ttl From P
Union All
Select e Ttl From P
)
#3
Select Sum(Ttl) From(
Select a+b Ttl From Q
Union All
Select d+e Ttl From P
)
Select a+b Ttl From Q
Union All
Select d+e Ttl From P
)
#4
Ttl 是什么东西
#5
还有 因为 用的是 sybase 不能用嵌套 ,A 表 与 B表 没有关系
#6
两表之间如果没有关联,不如:
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (select sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
SQL> insert into q values(1, 2);
已创建 1 行。
SQL> insert into p values(100, 1);
已创建 1 行。
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (selec
t sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
104
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (select sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
SQL> insert into q values(1, 2);
已创建 1 行。
SQL> insert into p values(100, 1);
已创建 1 行。
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (selec
t sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
104
#7
不准 用 嵌套 语句 的话 ,可以吗 ?
#8
两个表有没有主键啊?
#9
没
#10
还有 我 这样写 为什么错
#11
不写关联条件,就是笛卡尔积了, 数据翻N倍
#12
select sum(nvl(t.a,0)+nvl(t.b,0)+nvl(tt.d,0)+nvl(tt.e,0))
(select row_number()over() aa,a.q,q.b from Q)t full join (select row_number()over() ,bb,p.d,p.e from P) tt
on t.aa=tt.bb
(select row_number()over() aa,a.q,q.b from Q)t full join (select row_number()over() ,bb,p.d,p.e from P) tt
on t.aa=tt.bb
#13
没有主键、没有关联,还不允许嵌套,还要一个sql搞定。
实在是搞不定。
实在是搞不定。
#14
建个视图?
#15
create table t1(a int,b int,c int);
insert into t1 values(1,11,1);
insert into t1 values(1,1,11);
insert into t1 values(3,1,2);
create table t2(d int,e int,f int);
insert into t2 values(1,1,1);
insert into t2 values(2,2,5);
insert into t2 values(3,4,2);
select sum(b+c)/count(distinct d||','||e||','||f)
+sum(e+f)/count(distinct a||','||b||','||c)
as result from t1,t2;
drop table t1;
drop table t2;
/*
RESULT
----------
42
*/
#16
select m.a+n.b total
from (select sum(Q.a+Q.b) a from Q) m,(select sum(P.d+P.e) b from P) n
你的做法会因为查两个表的时候,数据少的表会重复加,加到和数据多的表的行数一样
from (select sum(Q.a+Q.b) a from Q) m,(select sum(P.d+P.e) b from P) n
你的做法会因为查两个表的时候,数据少的表会重复加,加到和数据多的表的行数一样
#17
SQL> select sum(a)+sum(b)+sum(d)+sum(e) from t1,t2;
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL>
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL>
#18
笛卡尔积了 大姐姐,1楼问你Q与P的关联条件是什么,你说没有,那肯定select 出来什么样数据都有了。
至少有个你想要的结果吧,说出来听听啊,3个字段呢么不是 怎么也要来点关系把。
至少有个你想要的结果吧,说出来听听啊,3个字段呢么不是 怎么也要来点关系把。
#19
SQL> select sum(a)+sum(b)+sum(d)+sum(e) from t1,t2;
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL> 这是对的
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL> 这是对的
#20
接分是王道!
#21
#1
Q与P的关联条件是什么?
#2
如果仅是想把它们加总,可以这样:
Select Sum(Ttl) From(
Select a Ttl From Q
Union All
Select b Ttl From Q
Union All
Select d Ttl From P
Union All
Select e Ttl From P
)
Select Sum(Ttl) From(
Select a Ttl From Q
Union All
Select b Ttl From Q
Union All
Select d Ttl From P
Union All
Select e Ttl From P
)
#3
Select Sum(Ttl) From(
Select a+b Ttl From Q
Union All
Select d+e Ttl From P
)
Select a+b Ttl From Q
Union All
Select d+e Ttl From P
)
#4
Ttl 是什么东西
#5
还有 因为 用的是 sybase 不能用嵌套 ,A 表 与 B表 没有关系
#6
两表之间如果没有关联,不如:
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (select sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
SQL> insert into q values(1, 2);
已创建 1 行。
SQL> insert into p values(100, 1);
已创建 1 行。
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (selec
t sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
104
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (select sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
SQL> insert into q values(1, 2);
已创建 1 行。
SQL> insert into p values(100, 1);
已创建 1 行。
SQL> select sum(col1+col2) from (select sum(Q.a+Q.b) as col1 from Q) x, (selec
t sum(P.d+P.e) as col2 from P) y;
SUM(COL1+COL2)
--------------
104
#7
不准 用 嵌套 语句 的话 ,可以吗 ?
#8
两个表有没有主键啊?
#9
没
#10
还有 我 这样写 为什么错
#11
不写关联条件,就是笛卡尔积了, 数据翻N倍
#12
select sum(nvl(t.a,0)+nvl(t.b,0)+nvl(tt.d,0)+nvl(tt.e,0))
(select row_number()over() aa,a.q,q.b from Q)t full join (select row_number()over() ,bb,p.d,p.e from P) tt
on t.aa=tt.bb
(select row_number()over() aa,a.q,q.b from Q)t full join (select row_number()over() ,bb,p.d,p.e from P) tt
on t.aa=tt.bb
#13
没有主键、没有关联,还不允许嵌套,还要一个sql搞定。
实在是搞不定。
实在是搞不定。
#14
建个视图?
#15
create table t1(a int,b int,c int);
insert into t1 values(1,11,1);
insert into t1 values(1,1,11);
insert into t1 values(3,1,2);
create table t2(d int,e int,f int);
insert into t2 values(1,1,1);
insert into t2 values(2,2,5);
insert into t2 values(3,4,2);
select sum(b+c)/count(distinct d||','||e||','||f)
+sum(e+f)/count(distinct a||','||b||','||c)
as result from t1,t2;
drop table t1;
drop table t2;
/*
RESULT
----------
42
*/
#16
select m.a+n.b total
from (select sum(Q.a+Q.b) a from Q) m,(select sum(P.d+P.e) b from P) n
你的做法会因为查两个表的时候,数据少的表会重复加,加到和数据多的表的行数一样
from (select sum(Q.a+Q.b) a from Q) m,(select sum(P.d+P.e) b from P) n
你的做法会因为查两个表的时候,数据少的表会重复加,加到和数据多的表的行数一样
#17
SQL> select sum(a)+sum(b)+sum(d)+sum(e) from t1,t2;
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL>
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL>
#18
笛卡尔积了 大姐姐,1楼问你Q与P的关联条件是什么,你说没有,那肯定select 出来什么样数据都有了。
至少有个你想要的结果吧,说出来听听啊,3个字段呢么不是 怎么也要来点关系把。
至少有个你想要的结果吧,说出来听听啊,3个字段呢么不是 怎么也要来点关系把。
#19
SQL> select sum(a)+sum(b)+sum(d)+sum(e) from t1,t2;
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL> 这是对的
SUM(A)+SUM(B)+SUM(D)+SUM(E)
---------------------------
93
SQL> select sum(a+b) from t1;
SUM(A+B)
----------
18
SQL> select sum(d+e) from t2;
SUM(D+E)
----------
13
SQL> select sum(t) from
2 (select sum(a+b) t from t1
3 union
4 select sum(d+e) from t2
5 );
SUM(T)
----------
31
SQL> 这是对的
#20
接分是王道!