title: Dynamically predicting the time based on hourly average
body: <p>I have a table like this below</p>
  <pre><code>Date      Start Time  End Time    Hours  Capacity  Hourly Avg
  2021-03-12  09:00      11:00        2      2         1
  2021-03-13  09:00      12:00        3      6         2
  2021-03-14  09:00      11:00        2      4         2
  </code></pre>
  <p>I have a set of orders with me, So based on the above table i need to propagate and find out at what time the below orders will be processed.</p>
  <pre><code>Q.No. Order no
  1   &quot;S0001&quot;      
  2   &quot;S0009&quot; 
  3   &quot;S00010&quot; 
  4   &quot;S0023&quot; 
  5   &quot;S0022&quot; 
  6   &quot;S0021&quot; 
  7   &quot;S00201&quot; 
  8   &quot;SO7532&quot; 
  9   &quot;SO7567&quot; 
  10  &quot;SO7456&quot; 
  11  &quot;SO7447&quot; 
  12  &quot;SO7908&quot; 
  </code></pre>
  <p>For example, The expected output will be like below,Based on the hourly average on top i need to split and forecast a time when this will be processed.</p>
  <pre><code> Q.No. Order no    Expected Processing time
      1   &quot;S0001&quot;        2021-03-12 10:00:00
      2   &quot;S0009&quot;        2021-03-12 11:00:00
      3   &quot;S00010&quot;       2021-03-13 10:00:00 
      4   &quot;S0023&quot;        2021-03-13 10:00:00  
      5   &quot;S0022&quot;        2021-03-13 11:00:00  
      6   &quot;S0021&quot;        2021-03-13 11:00:00  
      7   &quot;S00201&quot;       2021-03-13 12:00:00  
      8   &quot;SO7532&quot;       2021-03-13 12:00:00  
      9   &quot;SO7567&quot;       2021-03-14 10:00:00   
      10  &quot;SO7456&quot;       2021-03-14 10:00:00    
      11  &quot;SO7447&quot;       2021-03-14 11:00:00    
      12  &quot;SO7908&quot;       2021-03-14 11:00:00    
  </code></pre>
  <p>I'm doing it with PostgreSQL version 11, But didn't able to do this dynamically.Any help on this will , Will be grateful.</p>
  <p>Below is a sample code i use to find the expected time of processing of the orders,But i have remaining x orders which cannot be processed today and need to be processed tomorrow and so on based on the capacity and the hourly average.</p>
  <p>Basically i need the below code to be recursive and do it same for other days.
  I'm stuck at doing it dynamically.Any solution for this ?</p>
  <pre><code> With orders_to_be_processed_estimation AS
      (
      select *,NTILE(orders_picked_today/hourly_average) 
  OVER(ORDER BY queue_no) as partitions
      from pending_orders_to_be_picked_details
      ORDER BY partitions,queue_no
      )
      SELECT *,NOW() + INTERVAL '1 hour' * partitions  
  FROM  orders_to_be_processed_estimation
  </code></pre>
  <p>Thanks you</p>