title: Is there any way to apply loop in a presto query
body: <p>My use case is to use the below presto view on top of the table to get daily count by subtracting the todays value with that of yesterday. If there is no data in the table then the view should dynamically consider next day value and then take the average for the missing day view.</p>
  
  <p>This is a presto query. I have taken only one field in the below query</p>
  
  <pre><code>CREATE OR REPLACE VIEW hive.facebook.post_metrics_daily AS
  SELECT
      a.post_id,
      a.page,
      a.dt,
      a.created_time,
      (
          COALESCE(
              (
                  CAST(a.likes AS integer) 
                  - IF(
                      (CAST(b.likes AS integer) IS NULL), 
                      0, 
                      CAST(b.likes AS integer)
                  )
              )
              , 0
          )
      ) likes
  FROM
      hive.facebook.post_metrics a                 
      LEFT JOIN hive.facebook.post_metrics b 
          ON  a.dt = (b.dt + INTERVAL  '+1' DAY)
          AND a.post_id = b.post_id 
          AND a.brandname = b.brandname
  WHERE a.dt = date'2019-09-10'
  </code></pre>
  
  <p>If the data is for 9th to 12th and 10th data is missing then the view should take 11th day data and take the avg of 9th and 11th to give the 10th view. How can It be done? Can this formula be applied in the query and If yes then how?</p>
  
  <p><code>(today-yesterday)/n+1</code> where <code>n</code> will be the number of days missing.</p>
  
  <p>This is the sample data for likes. In case of missing likes, I need avg likes and the number of days which is missing should be dynamically identified by the query..       </p>
  
  <pre><code> Date   Likes-org.  missing likes daily likes org.   expected likes
  2019-10-17  20487   20487         20487                  20487
  2019-10-18  25384   25384          4897                  4897
  2019-10-19  26817   26817          1433                  1433
  2019-10-20  27499   missing likes   682                   257
  2019-10-21  27854   missing likes   355                   258
  2019-10-22  27987   missing likes   133                   258
  2019-10-23  28065   missing likes   78                    258
  2019-10-24  28106   28106           41                    258
  2019-10-25  28134   28134           28                     28
  </code></pre>