日期:2014-05-17 浏览次数:20744 次
create table # (id int,CT INT)
insert #
select 1,1
union all select 2,2
union all select 3,24
union all select 4,41
;
declare @num int
select @num= 10;
;
with t1 as(
select a.id,CT/@num ctx from # a
WHERE CT/@num>0
),t2 as(
select a.* from # a where ct<@num
union all select id,@num from (
select ROW_NUMBER() OVER(partition BY a.id order by a.id)row,a.*
from t1 a, sys.sysobjects
)t where row<=ctx
union all select id,ct%@num from # a where ct>@num)
select * from t2 order by id,ct desc
drop table #