日期:2014-05-18  浏览次数:20586 次

一个将数据分段的显示的问题?
比较ID编号为
0001至NNNN

中间有些可能并不连续,就是说有些号可能并不存在。
现在我想每30个ID为一组,然后取出每个30个ID组的第1个号码为第30个号码,不知道应该如何做?
比如,假设0001-0030中间没有断号,而且连续,则我需要的就是

0001-00030

这种形式。
谢谢

------解决方案--------------------
Create proc groupT
@value int
as
declare @MaxValue int
declare @MaxValueT int

declare @test table(Rowid int)
insert @test select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 8
union all select 9
union all select 15
union all select 16

declare @result table(start int,[end] int,startA varchar(10),[endA] varchar(10))

select *,id=identity(int,1,1) into #tmp from @test order by rowid
select @MaxValue=max(id) from #tmp where id % @value=0
select @MaxValueT=max(id) from #tmp
if @MaxValue is null
insert @result select min(id) a,max(id) b, ltrim(min(id)) c,ltrim(max(id)) d from #tmp
else
begin
insert @result select id,0, ' ', ' ' from #tmp where id <=@MaxValue and id %@value=1 order by id
update @result set [end]=start+@value-1
if @MaxValue <@MaxValueT
insert @result select @MaxValue+1 a,@MaxValueT, ' ', ' '
end
update @result set starta=rowid from #tmp where start=id
update @result set [enda]=rowid from #tmp where [end]=id
select startA,endA from @result
drop table #tmp

============================
groupt 3