Note that there are some explanatory texts on larger screens.

plurals
  1. POGet Time difference between group of records
    primarykey
    data
    text
    <p>I have a table with the following structure</p> <pre><code>ID ActivityTime Status 19 2013-08-23 14:52 1 19 2013-08-23 14:50 1 19 2013-08-23 14:45 2 19 2013-08-23 14:35 2 19 2013-08-23 14:32 1 19 2013-08-23 14:30 1 19 2013-08-23 14:25 2 19 2013-08-23 14:22 2 53 2013-08-23 14:59 1 53 2013-08-23 14:56 1 53 2013-08-23 14:57 1 53 2013-08-23 14:52 2 53 2013-08-23 14:50 2 53 2013-08-23 14:49 2 53 2013-08-23 14:18 2 53 2013-08-23 14:30 1 </code></pre> <p>I want to calculate the total time difference for each ID against Status 2. For example ID 19 stayed on Status 2 from 14:35 To 14:50 (15 minutes), then 14:22 to 14:30 (8 minutes). So the total ID 19 stayed on Status 2 is 23 minutes. <strong>(In both cases I considered the minimum time for status 2 to status 1)</strong> <strong>The problem is</strong> how do I only include difference between Status where they are different in <strong>next row</strong>. </p> <p>For example I would exclude the first record in the table with the status 1 and pick the second row. Same is the case with Status 2. I would pick the minimum time, calculate their difference and then add them up for multiple groups of status against each channel. </p> <p>I have tried using CURSOR but can't seem to make it work. I am also pretty confident that I can achieve that in C# after getting all the data and then looping through it. But I am just wondering if there is a direct way of getting the information without a loop. I also created a <a href="http://sqlfiddle.com/#!3/07830/8" rel="nofollow">SQL fiddle</a> with data, but I can't seem to make it work. I am coming from C# background with basic knowledge of SQL. Would be glad if someone can help me out. </p>
    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.
 

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