Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is an application of windows functions (called analytic functions in Oracle).</p> <p>Your goal is to assign a "block number" to each sequence of stops. That is, all stops in a sequence (for a vehicle) will have the same block number, and this will be different from all other sequences of stops.</p> <p>Here is a way to assign the block number:</p> <ol> <li>Create a speed flag that says 1 when speed > 0 and 0 when speed = 0.</li> <li>Enumerate all the records where the speed flag = 1. These are "blocks".</li> <li>Do a self join to put each flag = 0 in a block (this requires grouping and taking the max blocknum).</li> <li>Summarize by duration or however you want.</li> </ol> <p>The following code is a sketch of what I mean. It won't solve your problem, because you are not clear about how to handle day breaks, what information you want to summarize, and it has an off-by-1 error (in each sequence of stops it includes the previous non-stop, if any).</p> <pre><code>with vd as ( select vd.*, (case when SpeedFlag = 1 then ROW_NUMBER() over (partition by id, SpeedFlag) end) as blocknum from ( select vd.*, (case when speed = 0 then 0 else 1 end) as SpeedFlag from vehicaldata vd ) vd ) select id, blocknum, COUNT(*) as numrecs, SUM(duration) as duration from ( select vd.id, vd.rtime, vd.duration, MAX(vdprev.blocknum) as blocknum from vd left outer join vd vdprev on vd.id = vdprev.id and vd.rtime &gt; vdprev.rtime group by vd.id, vd.rtime, vd.duration ) vd group by id, blocknum </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. 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