Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could probably have it much more simplified by implementing @ mysql variables. Pre-query to get an ID, and the ID of the next associated with it, then do a join to the original table TWICE (different aliases) to have proper values, then do whatever calc you need... So, the "LastID" column is whatever the @WasLastID value was IF it was within the same skill target ID qualification. If not, it gets set back to zero. AFTER THAT TEST is applied, then the @WasLastID gets the ID of the current record as BASIS of the next entry's test.</p> <pre><code>select t.id, t.SkillTargetID, t.UTC_DateTime, t.UTC_DateTime2, if( @WasLastTarget = t.SkillTargetID, @WasLastID, 0 ) LastID, @WasLastID := t.ID as tmpLastID, @WasLastTarget := t.SkillTargetID as tmpLastTarget from testdur t, ( select @WasLastID = 0, @WasLastTarget := 0 ) sqlvars order by t.skillTargetID, t.id </code></pre> <p>The order by will be applied to the result set before any of the @ variables are processed, so the above query would in essence create the following result ensuring all skill targets are properly sequenced when applying the @ last target comparisons...</p> <pre><code> +----+---------------+--------------+--------+------------+---------------+ | id | SkillTargetID | UTC_DateTime | LastID | tmpLastID | tmpLastTarget | +----+---------------+--------------+--------+------------+---------------+ | 1 | 5000 | 1323719341 | 0 | 1 | 5000 | 3 | 5000 | 1323719342 | 1 | 3 | 5000 | 5 | 5000 | 1323719343 | 3 | 5 | 5000 | 10 | 5000 | 1323719445 | 5 | 10 | 5000 | 13 | 5000 | 1323719445 | 10 | 13 | 5000 | 16 | 5000 | 1323719460 | 13 | 16 | 5000 | 17 | 5000 | 1323719460 | 16 | 17 | 5000 -- BREAK BETWEEN SKILL TARGET... | 2 | 5010 | 1323719341 | 0 | 2 | 5010 | 4 | 5010 | 1323719342 | 2 | 4 | 5010 | 7 | 5010 | 1323719350 | 4 | 7 | 5010 | 8 | 5010 | 1323719441 | 7 | 8 | 5010 | 9 | 5010 | 1323719444 | 8 | 9 | 5010 | 14 | 5010 | 1323719445 | 9 | 14 | 5010 -- BREAK BETWEEN SKILL TARGET... | 6 | 5055 | 1323719345 | 0 | 6 | 5055 | 11 | 5055 | 1323719445 | 6 | 11 | 5055 -- BREAK BETWEEN SKILL TARGET... | 12 | 5060 | 1323719445 | 0 | 12 | 5060 | 15 | 5060 | 1323719446 | 12 | 15 | 5060 | 18 | 5060 | 1323719500 | 15 | 18 | 5060 +----+---------------+--------------+--------+------------+---------------+ </code></pre> <p>Now, all that said and sample provided, you could then expand on it such as...</p> <pre><code>select PreQuery.*, CASE WHEN t1.UTC_DateTime IS NULL THEN 0 ELSE t2.UTC_DateTime - t1.UTC_DateTime END) AS Duration from ( above full select query ) as PreQuery LEFT JOIN testdur t1 on PreQuery.ID = t1.ID LEFT JOIN testdur t2 on PreQuery.LastID = t2.ID </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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