Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php:4991) in /www/htdocs/v030397/mysql-qotd/wp-includes/feed-rss2-comments.php on line 8
Comments on: Question 82: More solutions for Q80? http://mysql-qotd.casperia.net/archives/535 mysql 5.0/5.1 questions for learning purposes Fri, 06 Aug 2010 16:56:36 +0000 http://wordpress.org/?v=abc hourly 1 By: plogi http://mysql-qotd.casperia.net/archives/535/comment-page-1#comment-93 plogi Sun, 11 Jul 2010 11:56:36 +0000 http://mysql-qotd.casperia.net/?p=535#comment-93 <b> Yes, another way of solving the problem is using a stored proecedure. There you can use the traditional approach of reading the rows in order and store the previous values in variables. <small><pre> drop procedure if exists proc_q80; delimiter // CREATE PROCEDURE proc_q80 () BEGIN declare v_cnt bigint; declare v_prev_ts timestamp; declare v_ts timestamp; declare v_diff bigint; declare v_s_diff bigint; declare v_done smallint; declare c_vendlog CURSOR FOR SELECT sold_ts FROM vending_log order by sold_ts; DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = 1; OPEN c_vendlog; set v_prev_ts = null; set v_done = 0; set v_cnt = 0; set v_s_diff = 0; REPEAT FETCH c_vendlog INTO v_ts; IF v_done = 0 THEN set v_diff = unix_timestamp(v_ts) - unix_timestamp(v_prev_ts); IF v_diff is not null THEN set v_cnt = v_cnt + 1; set v_s_diff = v_s_diff + v_diff; END IF; set v_prev_ts = v_ts; END IF; UNTIL v_done = 1 END REPEAT; close c_vendlog; SELECT v_s_diff / v_cnt as avg_diff; END // delimiter ; </pre></small> </b>
Yes, another way of solving the problem is using a stored proecedure. There you can use the traditional approach of reading the rows in order and store the previous values in variables.

drop procedure if exists proc_q80;
delimiter //
CREATE PROCEDURE proc_q80 ()
BEGIN
  declare v_cnt bigint;
  declare v_prev_ts timestamp;
  declare v_ts timestamp;
  declare v_diff bigint;
  declare v_s_diff bigint;
  declare v_done smallint;
  declare c_vendlog CURSOR FOR SELECT sold_ts FROM vending_log order by sold_ts;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = 1;

  OPEN c_vendlog;

  set v_prev_ts = null;
  set v_done = 0;
  set v_cnt = 0;
  set v_s_diff = 0;
  REPEAT
    FETCH c_vendlog INTO v_ts;
    IF v_done = 0 THEN
       set v_diff = unix_timestamp(v_ts) - unix_timestamp(v_prev_ts);
       IF v_diff is not null THEN
          set v_cnt = v_cnt + 1;
          set v_s_diff = v_s_diff + v_diff;
       END IF;
       set v_prev_ts = v_ts;
    END IF;
  UNTIL v_done = 1 END REPEAT;
  close c_vendlog;
  SELECT v_s_diff / v_cnt as avg_diff;
END
//
delimiter ;


]]>