日期:2014-05-17 浏览次数:20431 次
declare @T table (rowid int identity(1,1),ID varchar(1),VAL int)
insert into @T
select 'A',55 union all
select 'A',51 union all
select 'B',40 union all
select 'B',50 union all
select 'B',107
;with maco as
(
select rowid,ID,VAL,ceiling(val/50.0) as c1,val%50 as c2 from @T
)
select a.rowid,a.ID,50 as VAL from maco a left join master..spt_values b
on 1=1
where a.c1=b.number and b.type='p' and b.number>0
union all
select rowid,ID,c2 from maco where c2<>0
order by rowid,val desc
/*
rowid ID VAL
----------- ---- -----------
1 A 50
1 A 5
2 A 50
2 A 1
3 B 50
3 B 40
4 B 50
5 B 50
5 B 7
(9 row(s) affected)
*/
declare @T table (ID varchar(1),VAL int)
insert into @T
select 'A',55 union all
select 'A',51 union all
select 'B',40 union all
select 'B',50 union all
select 'B',107
;with maco as
(
select ID,VAL,ceiling(val/50.0) as c1,val%50 as c2 from @T
)
select a.ID,50 as VAL from maco a left join master..spt_values b
on 1=1
where a.c1=b.number and b.type='p' and b.number>0
union all
select ID,c2 from maco where c2<>0
order by ID
/*
ID VAL
---- -----------
A 50
A 50
A 5
A 1
B 40
B 7
B 50
B 5