表order:
id user production amount
1 张三 手机1 10
2 李四 小灵通1 20
3 王二 手机2 10
4 张三 小灵通2 15
5 张三 手机1 20
6 张三 手机1 10
7 张三 手机1 10
8 李四 小灵通1 10汇总要求,如果按照id排序紧邻的两条记录如果user和
production完全一样就合并,如果不一样就不合并,就象这样:
结果:
user production amount
张三 手机1 10
李四 小灵通1 20
王二 手机2 10
张三 小灵通2 15
张三 手机1 40<----这里合并了
李四 小灵通1 10<----这里不能和前面的合并,因为数据不在一起本来是再大富翁论坛上的一个帖子,没有人回答.我想了很久,一直没有解决.
现在的想法是用游标逐条读取并且对比user 和 production 如果相同,sum().
但是能力有限,几天了没有解决.拜求各位高人!!!!
id user production amount
1 张三 手机1 10
2 李四 小灵通1 20
3 王二 手机2 10
4 张三 小灵通2 15
5 张三 手机1 20
6 张三 手机1 10
7 张三 手机1 10
8 李四 小灵通1 10汇总要求,如果按照id排序紧邻的两条记录如果user和
production完全一样就合并,如果不一样就不合并,就象这样:
结果:
user production amount
张三 手机1 10
李四 小灵通1 20
王二 手机2 10
张三 小灵通2 15
张三 手机1 40<----这里合并了
李四 小灵通1 10<----这里不能和前面的合并,因为数据不在一起本来是再大富翁论坛上的一个帖子,没有人回答.我想了很久,一直没有解决.
现在的想法是用游标逐条读取并且对比user 和 production 如果相同,sum().
但是能力有限,几天了没有解决.拜求各位高人!!!!
declare @t table(ID int identity(1,1),[user] varchar(10),production varchar(20),amount int)
insert into @t select '张三','手机1',10
union all select '李四','小灵通1',20
union all select '王二','手机2',10
union all select '张三','小灵通2',15
union all select '张三','手机1',20
union all select '张三','手机1',10
union all select '张三','手机1',10
union all select '李四','小灵通1',10--临时表处理
select * into #1 from @t A where exists (select 1 from @t where [user]=a.[user] and production=a.production
and (ID=a.ID-1 or ID=a.ID+1) )
select * into #2 from @t A where not exists (select 1 from @t where [user]=a.[user] and production=a.production
and (ID=a.ID-1 or ID=a.ID+1) )--查询
select ID,[user], production,amount from #2
union all
select ID=min(ID),[user], production,amount=sum(amount) from #1
group by [user], production
order by ID--结果
/*
ID user production amount
----------- ---------- -------------------- -----------
1 张三 手机1 10
2 李四 小灵通1 20
3 王二 手机2 10
4 张三 小灵通2 15
5 张三 手机1 40
8 李四 小灵通1 10(6 行受影响)
*/--删除测试环境
Drop table #1,#2
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 6,'张三','手机1 ',10
insert into @t select 7,'张三','手机1 ',10
insert into @t select 8,'李四','小灵通1',10
select
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=c.id-1 and [user]=c.[user] and production=c.production)) a,
(select d.* from @t d where not exists(select 1 from @t where id=d.id+1 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 40
李四 小灵通1 10
*/
select c.* from @t c where not exists(select 1 from @t where id=c.id-1 and [user]=c.[user] and production=c.production)--取每一段连续记录的截止记录,其特征是不存在id等于该记录id+1且[user]和production与该记录相等的记录
select d.* from @t d where not exists(select 1 from @t where id=d.id+1 and [user]=d.[user] and production=d.production)--取得以上记录之后,因为起始ID与截止ID必然为小于等于关系,取每一个起始ID对应的最小的截止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 6, '张三', '手机1', 20
insert into A select 7, '张三', '手机1', 20
insert into A select 8,'李四','小灵通1',20--select * from Adeclare @tempid int
select @tempid=min(id)-1 from Adeclare @tempus varchar(20)
set @tempus=''
declare @tempprd varchar(20)
set @tempprd=''
declare @id int
declare @us varchar(20)
declare @prd varchar(20)
declare mycur cursor for select id,us,production from A
open mycur
fetch next from mycur into @id,@us,@prd
while(@@fetch_status=0)
begin
if(@id=@tempid+1)
begin
set @tempid=@tempid+1
if(@us=@tempus) and (@prd=@tempprd)
delete from A where id=@id
else
begin
set @tempus=@us
set @tempprd=@prd
end
end
else
set @id=@tempid
fetch next from mycur into @id,@us,@prd
end
close mycur
deallocate mycur
反正思路一样,用exists,not exists
缺号的情况应该也是可以解决的!!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 5, '张三', '小灵通2',15
insert into A select 6, '张三', '手机1', 20
insert into A select 7, '张三', '手机1', 10
insert into A select 8, '张三', '手机1', 10
insert into A select 10,'张三', '手机1', 10insert into A select 11,'李四','小灵通1',20declare @tempid int
select @tempid=min(id)-1 from Adeclare @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(@id=@tempid+1)
begin
set @tempid=@tempid+1
if(@us=@tempus) and (@prd=@tempprd)
begin
set @num=@num+@amount
update A set amount=@num where id=@id
delete from A where id=@id-1
end
else
begin
set @tempus=@us
set @tempprd=@prd
set @num=@amount
end
end
else
set @tempid=@id
fetch next from mycur into @id,@us,@prd,@amount
end
close mycur
deallocate mycur
结果如下:
1 张三 手机1 10
2 李四 小灵通1 20
3 王二 手机2 10
5 张三 小灵通2 15
8 张三 手机1 40
10 张三 手机1 10
11 李四 小灵通1 20
因为我原来的问题已经解决了!!!!哈哈,但是新的问题我们可以继续研究,
因为大家如果日后遇到这个问题也号解决!!!
谢谢!!!
libin_ftsafe(子陌红尘) //zlp321002 //Yang_(扬帆破浪) //yuweiwei(YWW(杨思))
Sunyo00 //nekiy(蓝之焰)