create table orderline_arrivals
(
orderlineid number not null,
arrival_starttime date not null check ( mod(to_number(to_char(arrival_starttime, 'MI')), 5) = 0 ),
arrival_stoptime date not null check ( mod(to_number(to_char(arrival_stoptime, 'MI')), 5) = 0 ),
article_id varchar2(10) not null,
quantity number(4, 0) not null check ( quantity > 0 ),
check ( trunc(arrival_starttime) = trunc(arrival_stoptime) ),
check ( arrival_starttime < arrival_stoptime ),
primary key ( orderlineid )
)
Table created.
insert into orderline_arrivals (
orderlineid, arrival_starttime, arrival_stoptime, article_id, quantity
)
select 1, to_date('2021-03-18 08:00', 'YYYY-MM-DD HH24:MI:SS'), to_date('2021-03-18 08:15', 'YYYY-MM-DD HH24:MI:SS'), 'A1', 10 from dual union all
select 2, to_date('2021-03-18 08:00', 'YYYY-MM-DD HH24:MI:SS'), to_date('2021-03-18 08:15', 'YYYY-MM-DD HH24:MI:SS'), 'A2', 20 from dual union all
select 3, to_date('2021-03-18 08:10', 'YYYY-MM-DD HH24:MI:SS'), to_date('2021-03-18 08:20', 'YYYY-MM-DD HH24:MI:SS'), 'A1', 5 from dual union all
select 4, to_date('2021-03-18 08:30', 'YYYY-MM-DD HH24:MI:SS'), to_date('2021-03-18 08:40', 'YYYY-MM-DD HH24:MI:SS'), 'A3', 50 from dual
4 row(s) inserted.
commit
Statement processed.
select
to_char(slottime, 'YYYY-MM-DD') as day
, to_char(slottime, 'HH24:MI') || '-' || to_char(slottime + interval '5' minute, 'HH24:MI') as slot
, article_id
, sum(quantity) as quantity
from (
select
s.slottime
, case when s.slotno <= a.slots then a.article_id else null end as article_id
, case
when s.slotno <= mod(a.quantity, a.slots) then trunc(a.quantity / a.slots) + 1
when s.slotno <= a.slots then trunc(a.quantity / a.slots)
else null
end as quantity
from (
select
arrival_starttime
, round((arrival_stoptime - arrival_starttime) * (24*60/5)) as slots
, nvl(round((lead(arrival_starttime) over (
partition by trunc(arrival_starttime)
order by arrival_starttime
) - arrival_starttime) * (24*60/5)), 0) as slots_to_next
, article_id
, quantity
from orderline_arrivals oa
) a
cross apply (
select
level as slotno
, a.arrival_starttime + numtodsinterval(5 * (level-1), 'minute') as slottime
from dual
connect by level <= greatest(a.slots, a.slots_to_next)
) s
)
group by slottime, article_id
order by slottime, article_id
DAY | SLOT | ARTICLE_ID | QUANTITY | 2021-03-18 | 08:00-08:05 | A1 | 4 | 2021-03-18 | 08:00-08:05 | A2 | 7 | 2021-03-18 | 08:05-08:10 | A1 | 3 | 2021-03-18 | 08:05-08:10 | A2 | 7 | 2021-03-18 | 08:10-08:15 | A1 | 6 | 2021-03-18 | 08:10-08:15 | A2 | 6 | 2021-03-18 | 08:15-08:20 | A1 | 2 | 2021-03-18 | 08:20-08:25 | - | - | 2021-03-18 | 08:25-08:30 | - | - | 2021-03-18 | 08:30-08:35 | A3 | 25 | 2021-03-18 | 08:35-08:40 | A3 | 25 |
---|