title: T-SQL / SQL Server : help to calculate turnover time for account to become positve from negative balance
body: <p>I need help to calculate turnover time of any account to become positive from negative daily balance. For example, account 12345 is positive on 04/05/2013, but has negative balance on 04/06, 04/07, 04/08 (three days) and on fourth day it became positive. I would like to develop a query to calculate that turnover time (4 days).</p>
  
  <pre><code>Account Number  Transaction Date    Daily Balance
      12345       4/1/2013            304
      12345       4/2/2013            -78
      12345       4/3/2013            -65
      12345       4/4/2013            12
      12345       4/5/2013            25
      12345       4/6/2013            -345
      12345       4/7/2013            -450
      12345       4/8/2013            -650
      12345       4/9/2013            105
      12345       4/10/2013           110
      110000      4/1/2013            150
      110000      4/2/2013            -15
      110000      4/3/2013            -56
      110000      4/4/2013            -35
      110000      4/5/2013            -15
      110000      4/6/2013            106
      110000      4/7/2013            500
      110000      4/8/2013            -150
      110000      4/9/2013            50
      110000      4/10/2013           100
      55544       4/1/2013            150
      55544       4/2/2013            120
      55544       4/3/2013            -145
      55544       4/4/2013            -250
      55544       4/5/2013            15
      55544       4/6/2013            20
      55544       4/7/2013            40
      55544       4/8/2013            50
      55544       4/9/2013            -10
      55544       4/10/2013           60
  </code></pre>
  
  <p>I am trying to get following result from above table. The query will apply to transactions table having thousands of rows. Hence, I need help to build some efficient query to calculate following results. </p>
  
  <pre><code>Account Number  Turnover time for an account to become positive
  12345           3
  12345           4
  110000          5
  110000          2
  55544           3
  55544           2
  </code></pre>
  
  <p>I am using MS SQL server version 2008.</p>
  
  <p>Yes there are zeros. and they are consider as positive. I mean when balance is turning from negative to 0 or positive, it will be consider as turnover.</p>
  
  <p>The solution provided by Fadi Hassan is the most efficient and easy to implement. Although there is one challenge with it as when account balance is negative in begining, it doesn't work. For example, when balance is -110, -40, 0, 10, 90, 30, 0, -10, -20, 0. It will not work. </p>
  
  <p><img src="https://i.stack.imgur.com/JcVJN.jpg" alt="enter image description here"></p>
  
  <p>If somebody have better solution, kindly help.</p>