表格如下
id a b c
1 12 19 0
2 15 17 0
5 11 32 0
6 18 79 0
3 23 342 0
4 134 545 0结果:
id a b c
1 12 19 0
2 15 17 228
5 11 32 81379
6 18 79 81731
3 23 342 483
4 134 545 8349
说明
ID=2的C列值为ID=1行的A*B+C
ID=3的C列值为ID=2行的A*B+C
ID=6的C列值为ID=5行的A*B+C
依次至最后一行------------
以你的计算方式:
c4 = a3*b3+a2*b2+a1*b1;
如果写SQL语句会很复杂,不如用一个函数解决。
函数如下:create or replace function haha(id number) return number
is
rs number;
a number;
b number;
begin
rs:=0;
for i in 1..id-1 loop
select a,b into a,b from test te where te.id = i;
rs :=rs+a*b;
end loop;
return rs;
end haha;
最后的查询语句为:
select te.id,te.a,te.b ,haha(te.id) c from test;
id a b c
1 12 19 0
2 15 17 0
5 11 32 0
6 18 79 0
3 23 342 0
4 134 545 0结果:
id a b c
1 12 19 0
2 15 17 228
5 11 32 81379
6 18 79 81731
3 23 342 483
4 134 545 8349
说明
ID=2的C列值为ID=1行的A*B+C
ID=3的C列值为ID=2行的A*B+C
ID=6的C列值为ID=5行的A*B+C
依次至最后一行------------
以你的计算方式:
c4 = a3*b3+a2*b2+a1*b1;
如果写SQL语句会很复杂,不如用一个函数解决。
函数如下:create or replace function haha(id number) return number
is
rs number;
a number;
b number;
begin
rs:=0;
for i in 1..id-1 loop
select a,b into a,b from test te where te.id = i;
rs :=rs+a*b;
end loop;
return rs;
end haha;
最后的查询语句为:
select te.id,te.a,te.b ,haha(te.id) c from test;
解决方案 »
- ORACLE11g 我做了Create pfile from spfile;操作后,就无法找到SID了,我该如何做?
- oracle 小问题 散分了
- windows 7 64位操作系统,数据库为oralce 11g,vs2010不能连接上
- 能否有朋友将11g系统自身的定时计划解释一下?
- 请教Oracle高手!
- oracle和sqlserver在连接上的差别
- 请看这个错误
- 很急!!!!保存时,提示我 with grant option where子句有问题。怎么回事呀?with grant option 在近义词中代表什么?
- 求助,oracle无法连接
- ORACLE触发器问题
- oracle逐级向上汇总问题
- oracle自定义函数
其实分析函数就解决了的,当然函数也可以的
--自定义函数可以简单点
create or replace function haha(id number)
return number
is
rs number:=0;
begin
select sum(a*b) into rs from test where id < i;
return rs;
end haha;
--
create or replace function haha(i number) --这里是参数i
return number
is
rs number:=0;
begin
begin
select sum(a*b) into rs from test where id < i;
exception when others then
raise_application_error(-20120,'** haha() error **');
end;
return rs;
end haha;