"James Walker" <walker@modernfold.com> wrote in message
news:%232sOjRBgGHA.2188@TK2MSFTNGP04.phx.gbl...
Ok, this is a fun one. Here's a simple solution, using intiger division of
the running total by the number of hours per day. But I worry that the
assignment of orders to days is too "dumb". I have a suspicion that there's
a better solution somewhere that would sequence the work to fill up the days
and minimize idle time.
create table Orders
(
OrderNumber int primary key,
TotalUnits int not null
)
insert into Orders(orderNumber,TotalUnits)
select 101,26
union all select 102,11
union all select 103,9
union all select 104,8
union all select 105,16
go
declare @days int,
@dailyUnits int
set @days = 3
set @DailyUnits = 30;
with UpcomingOrders(OrderNumber,TotalUnits,RunningTotal) as
(
select OrderNumber, TotalUnits,
(select sum(TotalUnits) from Orders where OrderNumber <= o.OrderNumber)
RunningTotal
from Orders o
)
select
Ordernumber,
TotalUnits,
1+RunningTotal/@DailyUnits WorkDay,
row_number() over (partition by RunningTotal/@DailyUnits order by
OrderNumber) SequenceOrder
from UpcomingOrders
where 1+RunningTotal/@DailyUnits <= @days
David
|