title: Mysql Stored Procedure
body: <p>Hi i am trying to run a a loop in stored procedure where in i get the sum of the values from the select query based upon the <code>date(curr_date)</code>.
  I need to loop the date column in my select <code>query(curr_date)</code>.
  The problem that i am facing is that when i run the while loop it only gets me the 1st entry for the date from the database.</p>
  
  <p>My Mysql stored procedure is as follows:</p>
  
  <pre><code>CREATE PROCEDURE `paf_calculation`(out avg_sum1 float(15,3))
  BEGIN
      DECLARE day_count int;
      DECLARE extra_days int;
      DECLARE lookup_weeks int;
      DECLARE week_days INT;
      DECLARE count decimal;
      DECLARE datapoints int;
      DECLARE start_date date;
      DECLARE previous_days date; 
      DECLARE sum_extra_days float(15,3);
      DECLARE roid int;
      DECLARE historylookup_weeks int;    
      DECLARE avg_sum float;
      DECLARE i int;
      DECLARE curr_date date;
  
      set i = 0;
      SET WEEK_DAYS = 7;
  
      Set count = 0.5;
  
  
  
      select datediff('2016-10-10','2016-10-7') into historylookup_weeks;
  
  
      /** counts the number of days from the current week start and end days */
      select datediff('2016-10-10','2016-10-7') into day_count;
  
      /**No of extra days if the current week is more or less than 7 */
      SET extra_days = day_count - WEEK_DAYS;
  
      /**calculates the date 56 days from the current start week date */
      select date_sub('2016-10-17',interval 56 day) into previous_days ;
  
      /**calculates the datapoints*/ 
      set avg_sum1=0;
  
      Set datapoints = day_count * lookup_weeks; 
      set i = 0;
  
      set curr_date = '2016-10-08'; 
  
  
      while curr_date &lt; '2016-10-17' do
      SELECT SUM(actual_volume) into avg_sum FROM volume_trackers WHERE (date_inserted between '2016-08-23' AND '2016-10-17') AND (day_of_week in (dayofweek(curr_date))) and (ro_id=13);
      set curr_date=date_add(curr_date,interval 1 day);
      set avg_sum1=avg_sum+avg_sum1;
      END WHILE;  
  
  END
  </code></pre>