1) select a.id,sum(b.a)
from table a join table b
on a.id>=b.id
group by a.id
2)select a.id,isnull(a.a+b.a,a.a)
from table a left join table b
on a.id=b.id+1orselect id,isnull(a+(select a from #t where id=(select max(id) from #t where id<a.id)),a)
from #t a
from table a join table b
on a.id>=b.id
group by a.id
2)select a.id,isnull(a.a+b.a,a.a)
from table a left join table b
on a.id=b.id+1orselect id,isnull(a+(select a from #t where id=(select max(id) from #t where id<a.id)),a)
from #t a
select id,a+isnull((select a from 表 where id=aa.id-1),0) from 表 aa2、
select id,a+isnull((select a from 表 where id<aa.id),0) from 表 aa
select id,a+isnull((select a from 表 where id=aa.id-1),0) from 表 aa1、
select id,a+isnull((select a from 表 where id<aa.id),0) from 表 aa
这个会
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
select id,a+isnull((select sum(a) from 表 where id<aa.id),0) from 表 aa2、
select id,a+isnull((select a from 表 where id=aa.id-1),0) from 表 aa
1、
select id,a+isnull((select a from 表 where id<aa.id),0) from 表 aa
2、
select id,a+isnull((select top 1 a from 表 where id<aa.id order by id desc),0) from 表 aa
id a
1 10
3 13
4 14