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 80: How do you read between the rows? http://mysql-qotd.casperia.net/archives/528 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/528/comment-page-1#comment-97 plogi Thu, 15 Jul 2010 16:25:06 +0000 http://mysql-qotd.casperia.net/?p=528#comment-97 <b> Aaaaand still a subquery solution: <pre> select avg(unix_timestamp(v1.sold_ts)-unix_timestamp(v2.sold_ts)) as avg_diff from vending_log v1, vending_log v2 where v2.sold_ts = (select sold_ts from vending_log where sold_ts < v1.sold_ts order by 1 desc limit 1 ); </pre> <b /></pre></b>
Aaaaand still a subquery solution:

select avg(unix_timestamp(v1.sold_ts)-unix_timestamp(v2.sold_ts)) as avg_diff
from vending_log v1, vending_log v2 where v2.sold_ts =
(select sold_ts from vending_log
  where sold_ts < v1.sold_ts
  order by 1 desc limit 1 );

]]>
By: plogi http://mysql-qotd.casperia.net/archives/528/comment-page-1#comment-96 plogi Thu, 15 Jul 2010 10:39:47 +0000 http://mysql-qotd.casperia.net/?p=528#comment-96 <b> Here is still a variation on the subquery solution... Instead of selecting the max(), you can also just select 1 timestamp and limit by 1. I'm not fond of "limit 1" for anything else than testing/debugging, but in this case it is faster than the "select max()" solution. <small><pre> select avg(vv1.diff) from (select *,unix_timestamp(v1.sold_ts) - unix_timestamp((select sold_ts from vending_log where sold_ts < v1 .sold_ts order by sold_ts desc limit 1)) as diff from vending_log v1) vv1; </pre></pre></small> </b>
Here is still a variation on the subquery solution…
Instead of selecting the max(), you can also just select 1 timestamp and limit by 1.
I’m not fond of “limit 1″ for anything else than testing/debugging, but in this case it is faster than the “select max()” solution.
select avg(vv1.diff) from
  (select *,unix_timestamp(v1.sold_ts) -
            unix_timestamp((select sold_ts
                            from vending_log
                            where sold_ts < v1 .sold_ts order by sold_ts desc limit 1)) as diff
   from vending_log v1) vv1;


]]>
By: plogi http://mysql-qotd.casperia.net/archives/528/comment-page-1#comment-91 plogi Fri, 09 Jul 2010 21:26:52 +0000 http://mysql-qotd.casperia.net/?p=528#comment-91 <b> Mysql does not have OLAP extensions or windowing functions at this point. So it is not that easy to refer to the previous row. Below is one way of solving the problem: With a subquery to retrieve the previous row. It works quite fine and is easy to read, but it is slow. <small><pre> select avg(vv1.diff) from (select *,unix_timestamp(v1.sold_ts) - unix_timestamp((select max(sold_ts) from vending_log where sold_ts < v1 .sold_ts)) as diff from vending_log v1) vv1; </pre></pre></small> The next one is fast, but relies on user variables. <small><pre> set @a:=null; set @b:=null; select avg(vv1.diff) from (select @b:=@a,@a:=sold_ts, unix_timestamp(sold_ts) - unix_timestamp(@b) as diff from vending_log order by sold_ts) vv1; </pre></small> </b>
Mysql does not have OLAP extensions or windowing functions at this point. So it is not that easy to refer to the previous row.
Below is one way of solving the problem: With a subquery to retrieve the previous row. It works quite fine and is easy to read, but it is slow.

select avg(vv1.diff) from
  (select *,unix_timestamp(v1.sold_ts) -
            unix_timestamp((select max(sold_ts)
                            from vending_log
                            where sold_ts < v1 .sold_ts)) as diff
   from vending_log v1) vv1;

The next one is fast, but relies on user variables.

set @a:=null;
set @b:=null;
select avg(vv1.diff) from
  (select @b:=@a,@a:=sold_ts, unix_timestamp(sold_ts) - unix_timestamp(@b) as diff
   from vending_log order by sold_ts) vv1;


]]>