sql数字运算
create table #a(id int,item int,a1 int)
insert into #a values(1,100,80)
insert into #a values(2,100,70)
insert into #a values(3,100,60)
insert into #a values(4,200,30)
insert into #a values(5,200,8)
insert into #a values(6,250,230)
select * from #a
我要得到的结果为如下
id item a1 jieguo
1 100 80 80
2 100 70 150
3 100 60 210
4 200 30 30
5 200 8 38
6 250 230 230
运算逻辑
item 相同,id 有比现在小的,就把a1 累加
------解决方案--------------------SELECT A.id,a.item,a.a1,SUM(b.a1) AS jieguo
FROM #a A
INNER JOIN #a B ON A.item = B.item AND A.a1<=B.a1
GROUP BY a.id,a.item,a.a1
/*
id item a1 jieguo
1 100 80 80
2 100 70 150
3 100 60 210
4 200 30 30
5 200 8 38
6 250 230 230*/