Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm struggling with the ActiveAtEnd value, but the others are OK.</p> <p>This is for MySQL:</p> <pre><code>set @active:=0; select period, sum( if( score=1, 1, 0)) New, if( max(ab) &gt; max(aa), max(ab), max(aa)) MaxActive, if( min( ab ) &lt; min( aa ), min(ab), min(aa)) MinActive from ( select period, etime, score, @active ab, @active:=@active+score aa from ( select from_unixtime( floor( unix_timestamp(start)/600) * 600) period, start etime, +1 score from ev union all select from_unixtime( floor( unix_timestamp(end)/600) * 600) period, end etime, -1 score from ev ) event order by etime ) as temp group by period; </code></pre> <p>The innermost selection breaks the original table into a set of events - with a score of +1 for a start-event, and -1 for an end event. union all is used so that duplicate events are allowed.</p> <p>The next inner selection runs a variable across the score values - @active holds a count of the number of active intervals at each time point. Both the value of @active before and after the current count is added is selected: I do not know how portable this is.</p> <p>The outermost selection accumulates the results for each period. 'New' is the sum of '+1' scores, MaxActive and MinActive must both take the value of active before (ab) and active after (aa) into account.</p> <p>Here are sample results:</p> <pre><code>+---------------------+------+-----------+-----------+ | period | New | MaxActive | MinActive | +---------------------+------+-----------+-----------+ | 2008-11-19 10:00:00 | 3 | 3 | 0 | | 2008-11-19 10:10:00 | 1 | 2 | 1 | | 2008-11-19 10:20:00 | 0 | 2 | 1 | | 2008-11-19 10:30:00 | 1 | 2 | 1 | | 2008-11-19 10:40:00 | 0 | 1 | 0 | +---------------------+------+-----------+-----------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload