好吧,我这样做更多是为了挑战,而不是因为我认为这是个好主意。我倾向于相信 Aaron 光标可能更合适。无论如何:
declare @Items table (ID int not null,LENGTH_IN_CM decimal(5,1) not null)
insert into @Items(ID,LENGTH_IN_CM) values
(1,1.0),
(2,1.0),
(3,9.0),
(4,5.0),
(5,15.0),
(6,3.0),
(7,6.0)
;With PossiblePages as (
select ID as MinID,ID as MaxID,LENGTH_IN_CM as TotalLength from @Items
union all
select MinID,MaxID + 1,CONVERT(decimal(5,1),TotalLength + LENGTH_IN_CM)
from
PossiblePages pp
inner join
@Items it
on
pp.MaxID + 1 = it.ID
where
TotalLength + LENGTH_IN_CM <= 20.0
), LongPages as (
select MinID,MAX(MaxID) as MaxID,MAX(TotalLength) as TotalLength from PossiblePages group by MinID
), FinalPages as (
select MinID,MaxID,TotalLength from LongPages where MinID = 1
union all
select lp.MinID,lp.MaxID,lp.TotalLength
from
LongPages lp
inner join
FinalPages fp
on
lp.MinID = fp.MaxID + 1
), PageNumbers as (
select MinID,MaxID,ROW_NUMBER() OVER (ORDER BY MinID) as PageNo
from FinalPages
)
select * from PageNumbers
结果:
MinID MaxID PageNo
----------- ----------- --------------------
1 4 1
5 6 2
7 7 3
如果您想为每一行分配一个页码,这应该很容易加入到原始表中。
PossiblePages
计算每个可能的页面 - 对于每一行,它就像该行是该页面上的第一行一样,然后计算出可以附加多少额外的行,以及该行范围代表的总长度(可能有更简洁的方法来计算这个表达式,目前不确定)。
LongPages
PossiblePages
然后为每个起始行号找到计算的最长值。
最后,FinalPages
从第一页开始(从逻辑上讲,它必须是从 1 开始的ID
——如果你不能保证从 1 开始,你总是可以引入另一个计算,并且需要找到最早的)。然后,下一页是从比上一行高一个的行 ID 开始的页面。
你不需要 PageNumbers
,但正如我所说,我正在考虑加入原来的表。
正如评论者所预测的那样,我认为这不会很好 - 仅在样本上,我看到至少 4 个表扫描来计算它。
奖励精神错乱。这个只扫描表 3 次:
;With PageRows as (
select ID as MinID,ID as MaxID,LENGTH_IN_CM as TotalLength from @Items where ID=1
union all
select MinID,MaxID + 1,CONVERT(decimal(5,1),TotalLength + LENGTH_IN_CM)
from
PageRows pr
inner join
@Items ir
on
pr.MaxID = ir.ID-1
where
TotalLength + LENGTH_IN_CM <= 20.0
union all
select ir.ID as MinID,ir.ID as MaxID,ir.LENGTH_IN_CM as TotalLength
from
PageRows pr
inner join
@Items ir
on
pr.MaxID = ir.ID-1
where
TotalLength + LENGTH_IN_CM > 20.0
), PageNumbers as (
select MinID,MAX(MaxID) as MaxID,ROW_NUMBER() OVER (ORDER BY MinID) as PageNo
from PageRows
group by MinID
)
select * from PageNumbers