create table tab ( a varchar(2), b int, c int )insert into tab select 'A1', 1, 2 insert into tab select 'A2', 3, 3 insert into tab select 'A3', 2, 6 insert into tab select 'A4', 5, 8--语句 declare @a varchar(2) set @a = 'A4' declare @int int select top 1 @int = c from tab select @int = @int + b from tab where a < @a select @int@int值既是c的值
只需要知道A列的值,即可通过计算上一行B+C,得到相应C值drop table #temp create table #temp ( A varchar(2), B int, C int ) insert into #temp select 'A1', 1, 2 insert into #temp select 'A2', 3, 3 insert into #temp select 'A3', 2, 6 insert into #temp select 'A4', 5, 8假设知道A='A3' select a.B+a.C C from #temp a,#temp b where cast(right(a.A,1) as int)=cast(right(b.A,1) as int)-1 and b.A='A3' --- 6
create table tab
(
a varchar(2),
b int,
c int
)insert into tab select 'A1', 1, 2
insert into tab select 'A2', 3, 3
insert into tab select 'A3', 2, 6
insert into tab select 'A4', 5, 8--语句
declare @a varchar(2)
set @a = 'A4'
declare @int int
select top 1 @int = c from tab select @int = @int + b from tab where a < @a
select @int@int值既是c的值
create table #temp
(
A varchar(2),
B int,
C int
)
insert into #temp select 'A1', 1, 2
insert into #temp select 'A2', 3, 3
insert into #temp select 'A3', 2, 6
insert into #temp select 'A4', 5, 8假设知道A='A3'
select a.B+a.C C
from #temp a,#temp b
where cast(right(a.A,1) as int)=cast(right(b.A,1) as int)-1 and b.A='A3'
---
6