大家好,我想请问一个表,如何根据记录数的不同,如何分别分次累计不同记录数的不同总和。如
A B C
我 8-1 3
我 8-2 7
你 8-3 10
我 8-4 5
你 8-4 9要获得的结果A B C
我 8-1 3
我 8-2 10(=3+7)
你 8-3 10
我 8-4 15(=3+7+5)
你 8-4 19(=10+9)
A B C
我 8-1 3
我 8-2 7
你 8-3 10
我 8-4 5
你 8-4 9要获得的结果A B C
我 8-1 3
我 8-2 10(=3+7)
你 8-3 10
我 8-4 15(=3+7+5)
你 8-4 19(=10+9)
(取遍b的所有值)
if object_id('tb') is not null
drop table tb
go
create table tb(a varchar(2),b varchar(5),c int)
insert into tb select '我', '8-1', 3
union all select '我', '8-2', 7
union all select '你', '8-3', 10
union all select '我', '8-4', 5
union all select '你', '8-4', 9
--假设b列-后面只有一位数字
select a,b,
c=
(
select sum(c) from tb
where a=b.a
and abs(right(b,1))<=abs(right(b.b,1))
)
from tb b drop table tb
/*
(5 行受影响)
a b c
---- ----- -----------
我 8-1 3
我 8-2 10
你 8-3 10
我 8-4 15
你 8-4 19(5 行受影响)*/
if object_id('tb') is not null
drop table tb
go
create table tb(a varchar(2),b varchar(5),c int)
insert into tb select '我', '8-1', 3
union all select '我', '8-2', 7
union all select '你', '8-3', 10
union all select '我', '8-4', 5
union all select '你', '8-4', 9
--假设b列-后面只有一位数字
select a,b,
c=
(
select sum(c) from tb
where a=b.a
and abs(right(b,1)) <=abs(right(b.b,1))
)
from tb b drop table tb
/*
(5 行受影响)
a b c
---- ----- -----------
我 8-1 3
我 8-2 10
你 8-3 10
我 8-4 15
你 8-4 19 (5 行受影响) */
--g给点分吧,不要再发0分贴了