--看明白了.如果是你的表中没有主键,就得用临时表--下面是处理方法--排序处理
select *,gid=0 into #t from 表 order by a,b
declare @b int,@gid int
update #t set @gid=case @b when b then @gid+1 else 0 end
,gid=@gid,@b=bselect a,b from #t order by a,gid,bdrop table #t
select *,gid=0 into #t from 表 order by a,b
declare @b int,@gid int
update #t set @gid=case @b when b then @gid+1 else 0 end
,gid=@gid,@b=bselect a,b from #t order by a,gid,bdrop table #t
declare @t table(a int,b int)
insert into @t
select 1,1
union all select 1,1
union all select 1,2
union all select 1,3
union all select 2,1
union all select 2,2
union all select 2,3
union all select 3,2
union all select 3,2
union all select 3,3--排序处理
select *,gid=0 into #t from @t order by a,b
declare @b int,@gid int
update #t set @gid=case @b when b then @gid+1 else 0 end
,gid=@gid,@b=bselect a,b from #t order by a,gid,b--删除处理临时表
drop table #t/*--测试结果
a b
----------- -----------
1 1
1 2
1 3
1 1
2 1
2 2
2 3
3 2
3 3
3 2(所影响的行数为 10 行)
--*/
declare @t table(id int identity(1,1),a int,b int)
insert into @t
select 1,1
union all select 1,1
union all select 1,2
union all select 1,3
union all select 2,1
union all select 2,2
union all select 2,3
union all select 3,2
union all select 3,2
union all select 3,3--排序处理
select a,b
from @t a
order by a,(select count(*) from @t where a=a.a and b=a.b and id<=a.id),b/*--测试结果
a b
----------- -----------
1 1
1 2
1 3
1 1
2 1
2 2
2 3
3 2
3 3
3 2(所影响的行数为 10 行)
--*/