Oracle sum求和问题

jeenhuang 2009-11-04 03:46:01
orcale 执行下面
select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum<3

得到的结果是 315000 74000


注意,我对字段加了一个sum如下

select sum(ninnum) from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum<3

得到的结果不是上面的两个字相加389000,而是一个错误的值:310000


请问这是为什么呢????
...全文
384 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqb2008 2009-11-05
  • 打赏
  • 举报
回复
rownum的问题
雨青 2009-11-05
  • 打赏
  • 举报
回复
Rownum的问题导致。
先去掉看一下。会是如何
小灰狼W 2009-11-04
  • 打赏
  • 举报
回复
关注下
可能是由于排序的原因吧
inthirties 2009-11-04
  • 打赏
  • 举报
回复
elect sum(ninnum) from (select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3 )
inthirties 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 jeenhuang 的回复:]
orcale 执行下面
select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3

得到的结果是  315000  74000 


注意,我对字段加了一个sum如下

select sum(ninnum) from (select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3 )

得到的结果不是上面的两个字相加389000,而是一个错误的值:310000 


请问这是为什么呢????
[/Quote]

rownum导致的冤案。

如果要用rownuw写到子查询里

如下

Adebayor 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 jeenhuang 的回复:]
orcale 执行下面
select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3

得到的结果是  315000  74000 


注意,我对字段加了一个sum如下

selectsum(ninnum) from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3

得到的结果不是上面的两个字相加389000,而是一个错误的值:310000 


请问这是为什么呢????
[/Quote]
我想应该是rownum <3问题,而且你的两个表里面的有不规则数据吧
试下这个:
select sum(ninnum) from(
select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U'
) where rownum < 3;

jeenhuang 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sbaz 的回复:]
select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3

表明这里不只2条记录,你只看到了
315000  74000是因为你用了rownum <3的限制
实际上还有几条记录你看不到。

建议你2条sql都去掉rownum <3的限制。
[/Quote]



我做过测试,不是这个原因所在。
create table test1
(
id int,
name varchar(10)
)

create table test2
(
id int,
value number
)

insert into test1(id,name) values(1,'a');
insert into test1(id,name) values(2,'b');
insert into test1(id,name) values(3,'c');

insert into test2 values(1,10);
insert into test2 values(1,20);
insert into test2 values(1,30);
insert into test2 values(1,40);


select sum(b.value) from test1 a ,test2 b where a.id=+b.id and rownum<5
这是测试的,完全正确
jeenhuang 2009-11-04
  • 打赏
  • 举报
回复
结果和我以前的错误值一样
ngx20080110 2009-11-04
  • 打赏
  • 举报
回复
試一下

select sum(ninnum) from (
select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3
)
超叔csdn 2009-11-04
  • 打赏
  • 举报
回复
select ninnum from ic_general_h a, ic_general_b b where
a.cgeneralhid =+b.cgeneralhid and cdispatcherid ='0001AA1000000000GY9U' and rownum <3

表明这里不只2条记录,你只看到了
315000 74000是因为你用了rownum<3的限制
实际上还有几条记录你看不到。

建议你2条sql都去掉rownum<3的限制。
archwuke1 2009-11-04
  • 打赏
  • 举报
回复
个人认为数据库不应该出现这样的情况吧

等待高手的回答.!
jeenhuang 2009-11-04
  • 打赏
  • 举报
回复
补充内容:这个必须是ic_general_h为主表,求的是子表的和,由于其他业务关系,不能改变,除了一种在select 中的子查询可以解决问题外,这种方法速度太慢,请问有方法的说明方法。能说原因的也说一下,谢谢

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧