求字段数据相加的语句或存储过程 全部分了 谢谢
ID name sex age qunty qunty2
1姓名1115455.0500.0
2姓名2216334.0520.0
3姓名311734.0451.0
4姓名4218458.0522.0
5姓名5119435.0504.0
6姓名6220234.0560.0
7姓名7121345.0855.0
想的到的结果是
qunty2 = 本记录的qunty +上一条记录的 qunty2
qunty 可能为NULL
比如
qunty qunty2
455.0500.0
334.0520.0
34.0451.0
结果是
qunty qunty2
455.0455.0
334.0789
34.0823
请问高手怎么 解决
可以写 SQL语句 或存储过程
[解决办法]
select qunty,qunty2=(select sum(qunty)from ta where id!> a.id)
from ta as a
[解决办法]
declare @t table(ID int,qunty real,qunty2 real)
insert @t
select 1,455,500 union all
select 2,334,520 union all
select 3,34,451
select qunty,qunty2=(select sum(qunty)from @t where id <=a.id)
from @t as a
/*
quntyqunty2
455.0455.0
334.0789.0
34.0823.0
*/
[解决办法]
declare @t table(ID int,qunty real,qunty2 real)
insert @t
select 1,455,500 union all
select 2,334,520 union all
select 3,34,451
select id,isnull(a.qunty,0) qunty,
qunty2=isnull(a.qunty,0)+isnull((select qunty2 from @t where id=a.id-1),0)
from @t as a
-------
1 455.0 455.0
2 334.0 834.0
3 34.0 554.0
[解决办法]
--加一个判断就行了
select qunty,qunty2=(select sum(case when qunty is null then 0 else qunty end)from ta where id!> a.id)
from ta as a