Thursday, March 18, 2021

Here's a requirement I had to recently solve using SQL. I managed to come up with a solution, but it's a bit elaborate and cumbersome.

Without sharing my solution just now, I was wondering how you would solve this.

Given a table:

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)
/

Let's say we have following four tuples: 


 Per orderline we need to distribute the quantity over the 5-minute slots for that orderline:

For a given date, then the final resultset should look like this: