表order:
i duser production amount
1 张三 手机1 10
2 李四 小灵通1 20
3 王二 手机2 10
4 张三 小灵通2 15
5 张三 手机1 20
7 张三 手机1 20
9 张三 手机1 20
13 张三 手机1 10
14李四 小灵通1 10
15李四 小灵通1 10汇总要求,如果按照id排序紧邻的两条记录如果user和
production完全一样就合并,如果不一样就不合并,就象这样:
结果:
user production amount
张三 手机1 10
李四 小灵通1 20<----前面的
王二 手机2 10
张三 小灵通2 15
张三 手机1 70<----这里合并了
李四 小灵通1 20<----这里不能和前面的合并,因为数据不在一起前贴:libin_ftsafe(子陌红尘) //zlp321002 //Yang_(扬帆破浪) //yuweiwei(YWW(杨思))
Sunyo00 //nekiy(蓝之焰)
等高人做出了应有的回答,在此我先谢谢大家!!!!!
因为$$Yang_(扬帆破浪)$$
提出缺号的问题,我测试原帖都不能处理,因为大家都是根据ID号码来做的.
在原来情况下,这当然是最好的解决方案.
所以在这里来处理.
原帖位置:http://community.csdn.net/Expert/topic/4713/4713958.xml?temp=.7089197
谢谢大家发言!!!
i duser production amount
1 张三 手机1 10
2 李四 小灵通1 20
3 王二 手机2 10
4 张三 小灵通2 15
5 张三 手机1 20
7 张三 手机1 20
9 张三 手机1 20
13 张三 手机1 10
14李四 小灵通1 10
15李四 小灵通1 10汇总要求,如果按照id排序紧邻的两条记录如果user和
production完全一样就合并,如果不一样就不合并,就象这样:
结果:
user production amount
张三 手机1 10
李四 小灵通1 20<----前面的
王二 手机2 10
张三 小灵通2 15
张三 手机1 70<----这里合并了
李四 小灵通1 20<----这里不能和前面的合并,因为数据不在一起前贴:libin_ftsafe(子陌红尘) //zlp321002 //Yang_(扬帆破浪) //yuweiwei(YWW(杨思))
Sunyo00 //nekiy(蓝之焰)
等高人做出了应有的回答,在此我先谢谢大家!!!!!
因为$$Yang_(扬帆破浪)$$
提出缺号的问题,我测试原帖都不能处理,因为大家都是根据ID号码来做的.
在原来情况下,这当然是最好的解决方案.
所以在这里来处理.
原帖位置:http://community.csdn.net/Expert/topic/4713/4713958.xml?temp=.7089197
谢谢大家发言!!!
insert into @t select 1 ,'张三','手机1 ',10
insert into @t select 2 ,'李四','小灵通1',20
insert into @t select 3 ,'王二','手机2 ',10
insert into @t select 4 ,'张三','小灵通2',15
insert into @t select 5 ,'张三','手机1 ',20
insert into @t select 7 ,'张三','手机1 ',20
insert into @t select 9 ,'张三','手机1 ',20
insert into @t select 13,'张三','手机1 ',10
insert into @t select 14,'李四','小灵通1',10
insert into @t select 15,'李四','小灵通1',10select
e.[user],e.production,sum(f.amount) amount
from
(select
a.id,a.[user],a.production,min(b.id) as b_id
from
(select c.* from @t c where not exists(select 1 from @t where id=(select max(id) from @t where id<c.id) and [user]=c.[user] and production=c.production)) a,
(select d.* from @t d where not exists(select 1 from @t where id=(select min(id) from @t where id>d.id) and [user]=d.[user] and production=d.production)) b
where
a.[user]=b.[user] and a.id<=b.id
group by
a.id,a.[user],a.production) e,
@t f
where
f.id between e.id and e.b_id
group by
e.id,e.[user],e.production/*
user production amount
-------- ---------- -----------
张三 手机1 10
李四 小灵通1 20
王二 手机2 10
张三 小灵通2 15
张三 手机1 70
李四 小灵通1 20
*/
试试应该可以。
不连续的情况,取起始ID的标准是:不存在ID等于所有小于该记录ID的ID的最大值,且[user]和production与该记录相等的记录取截止ID的方法类似。
create table A (id int, us varchar(20), production varchar(20), amount int)
insert into A select 1 ,'张三','手机1 ',10
insert into A select 2 ,'李四','小灵通1',20
insert into A select 3 ,'王二','手机2 ',10
insert into A select 4 ,'张三','小灵通2',15
insert into A select 5 ,'张三','手机1 ',20
insert into A select 7 ,'张三','手机1 ',20
insert into A select 9 ,'张三','手机1 ',20
insert into A select 13,'张三','手机1 ',10
insert into A select 14,'李四','小灵通1',10
insert into A select 15,'李四','小灵通1',10declare @tempid int
select @tempid=min(id) from A
declare @tempus varchar(20)
set @tempus=''
declare @tempprd varchar(20)
set @tempprd=''
declare @num int
set @num=0
declare @amount int
declare @id int
declare @us varchar(20)
declare @prd varchar(20)
declare mycur cursor for select * from A
open mycur
fetch next from mycur into @id,@us,@prd,@amount
while(@@fetch_status=0)
begin
if(@us=@tempus) and (@prd=@tempprd)
begin
set @num=@num+@amount
update A set amount=@num where id=@id
delete from A where id=@tempid
set @tempid=@id
end
else
begin
set @tempus=@us
set @tempprd=@prd
set @num=@amount
set @tempid=@id
end
fetch next from mycur into @id,@us,@prd,@amount
end
close mycur
deallocate mycur select * from A
结果如下:
1 张三 手机1 10
2 李四 小灵通1 20
3 王二 手机2 10
4 张三 小灵通2 15
13 张三 手机1 70
15 李四 小灵通1 20