求助!!高手请进~~~~100分奉送!!!

heiguangbao 2003-06-19 09:52:57
我现在遇到一个比较严峻的问题,请高手解答
问题:字段a 字段b
1 7
3 8
2 4
5 9
实现的目的是:(字段b的第一条记录-字段a的第一条记录)/字段b的第一条记录,
(字段b的前两条记录-字段a的前两条记录)/字段b的前两条记录
一直这样循环下去
例如:(7-1)/7,((7+8)-(1+3))/(7+8),((7+8+4)-(1+3+2))/(7+8+4),
((7+8+4+9)-(1+3+2+5))/(7+8+4+9)
能用自定义函数实现吗?或者其他方法,先谢谢大家了!
...全文
128 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjb111 2003-06-20
  • 打赏
  • 举报
回复
select *, identity(int,1,1) as id into #t from addab

select a, b, (select (sum(cast(b as float)) - sum(cast(a as float))) / sum(cast(b as float)) from #t

where id <= A.id) result from #t as A
愉快的登山者 2003-06-20
  • 打赏
  • 举报
回复
select *, identity(int,1,1) as id into #t from yourtable

select a, b, (select (sum(cast(a as numeric(8,4)) - sum(cast(b as numeric(8,4))) / sum(cast(b as numeric(8,4)) from #t where id <= A.id) result from #t as A
愉快的登山者 2003-06-20
  • 打赏
  • 举报
回复
select *, identity(int,1,1) as id into #t from yourtable

select a, b, (select (sum(a) - sum(b)) / sum(b) from #t where id <= A.id) result from #t as A
hjb111 2003-06-20
  • 打赏
  • 举报
回复
create proc addab1
as
declare @aa int,@bb int
declare @sumab int,@addb int
declare @diva real
set @sumab=0
set @addb=0
declare a cursor for select a,b from addab

open a
fetch next from a into @aa,@bb
while @@fetch_status=0
begin
set @sumab=@sumab+@aa
set @addb=@addb+@bb
set @diva=(@addb-@sumab)/@addb
fetch next from a into @aa,@bb
end

close a
deallocate a
happydreamer 2003-06-19
  • 打赏
  • 举报
回复

select *,cast(0 as decimal(10,2)) as v into #r from a


declare @i decimal(10,4), @j decimal(10,4)
select @i=0,@j=0

update #r
set @i=@i+a,@j=@j+b,v=(@j-@i)/ @j
Methodor 2003-06-19
  • 打赏
  • 举报
回复
DBMS:通过临时表或游标可实现~
PB:利用datawindow实现起来更简单方便~
pengdali 2003-06-19
  • 打赏
  • 举报
回复
select identity(int,1,1) id,* from 你的表

select ((select sum(b) from 你的表 where id<=tem.id)-(select sum(a) from 你的表 where id<=tem.id))/(select sum(b) from 你的表 where id<=tem.id) 结果 from 你的表 tem
pengdali 2003-06-19
  • 打赏
  • 举报
回复
select identity(int,1,1) id,* from 你的表

select ((select sum(b) from 你的表 where id<=tem.id)-(select sum(a) from 你的表 where id<=tem.id))/(select sum(b) from 你的表 where id<=tem.id) 结果 from 表
CrazyFor 2003-06-19
  • 打赏
  • 举报
回复
更正一下,
declare @i numeric(28,6),@j numeric(28,6)
set @i=0
set @j=0
select *,case(0 as numeric(28,6)) as ValueCol into #temp from 表
update #temp
set ValueCol=((字段b+@j)-(字段a+@i))/(字段b+@j)
,@I=@i字段a
,@j=@j字段b
select * from #temp
CrazyFor 2003-06-19
  • 打赏
  • 举报
回复
declare @i numeric(28,6),@j numeric(28,6),@V numeric(28,6)
set @i=0,@j=0
select *,0 as ValueCol into #temp from 表
update #temp
set ValueCol=((字段b+@j)-(字段a+@i))/(字段b+@j)
,@I=@i字段a
,@j=@j字段b
select * from #temp

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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