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 81: More solutions for Q80? http://mysql-qotd.casperia.net/archives/533 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/533/comment-page-1#comment-92 plogi Sat, 10 Jul 2010 18:09:04 +0000 http://mysql-qotd.casperia.net/?p=533#comment-92 <b> Another solution for Q80 is using a helper table and a join. The helper table is created with the correct sort order and a surrogate key is added to support the join between the rows. <small><pre> drop table if exists log1; set @a:=0; create table log1 select @a:=@a+1 as row_id,v1.* from vending_log v1 order by sold_ts; alter table log1 add primary key(row_id); select avg(unix_timestamp(v1.sold_ts)-unix_timestamp(v2.sold_ts)) as avg_diff from log1 v1, log1 v2 where v2.row_id=v1.row_id - 1; </pre></small></b>
Another solution for Q80 is using a helper table and a join.
The helper table is created with the correct sort order and a surrogate key is added to support the join between the rows.

drop table if exists log1;
set @a:=0;
create table log1
  select @a:=@a+1 as row_id,v1.*
  from vending_log v1 order by sold_ts;
alter table log1 add primary key(row_id);

select avg(unix_timestamp(v1.sold_ts)-unix_timestamp(v2.sold_ts)) as avg_diff
from log1 v1, log1 v2
where v2.row_id=v1.row_id - 1;

]]>