title: MSSQL: calculate time be many starts and stops
body: <p>Is it possible to make this in sql:</p>
  
  <p>Have table with records:</p>
  
  <pre><code>    ID      LaborID     OrderNr      OrderStatusID  OrderStatusDate
  
  
      12990   3731573     OPT1814378          2       2018-05-28 09:35:30.123
      13105   230687389   OPT1814378          1       2018-05-29 10:32:14.850
      13106   230687389   OPT1814378          2       2018-05-29 10:52:14.403
      13123   230480202   OPT1814378          1       2018-05-29 13:18:05.233
      13130   230480202   OPT1814378          0       2018-05-29 13:29:17.360
      12837   3731573     OPT1814089          2       2018-05-25 20:28:24.817
      12906   10138504    OPT1814089          1       2018-05-26 10:41:18.680
      12909   10138504    OPT1814089          2       2018-05-26 10:57:40.733
      12913   10138504    OPT1814089          1       2018-05-26 11:41:48.387
      12920   10138504    OPT1814089          0       2018-05-26 12:15:48.590
  </code></pre>
  
  <p>where</p>
  
  <pre><code>OrderStatusID   
  0 - End
  1 - Begin
  2 - pause
  </code></pre>
  
  <p>Need calculate working time <code>from begin to pause</code> <code>(1-&gt;2)</code>  or <code>from begin to end</code> <code>(1-&gt;0)</code>. 
  My problem is that there are some conditions that I have to adhere to:</p>
  
  <p>If first record is <code>2</code> then ignore</p>
  
  <p>Work begin always with <code>1</code></p>
  
  <p>But can have more pause <code>(1-&gt;2)</code>
  The last work end record everytime with <code>0</code></p>
  
  <p>The result in this case will be:</p>
  
  <pre><code>OPT1814378          230687389   00:20:00      
  OPT1814378          230480202   00:11:12   
  OPT1814089          10138504    00:16:12
  OPT1814089          10138504    00:34:00
  </code></pre>