title: Oracle SQL running total on change of field (SUM on column only when field changes)
body: <p>I have a question in regards to how to SUM on a column only when a field is changing. </p>
  
  <p>Take for example the table below:</p>
  
  <p><strong>Note that Column A and Column B are different tables. I.e. A was selected from Table X and B was selected from Table Y</strong></p>
  
  <blockquote>
    <p>SELECT X.A, Y.B<br>
    FROM X<br>
    INNER JOIN Y ON X.DATE = Y.DATE AND X.VAL1 =
    Y.VAL1 AND X.VAL2 = Y.VAL2</p>
  </blockquote>
  
  <pre><code> A     B
  123    5
  123    5
  456    10
  789    15
  789    15
  </code></pre>
  
  <p>I need to sum column B on change of field on column A: </p>
  
  <p>I.e. the query should return <strong>5 + 10 + 15 = 30</strong> (5 the first time because value in column A is 123, 10 the second time because column A changed from 123 to 456 - note that the second row was skipped because column A still contains value 123 - hence the change of field logic and so on).</p>
  
  <p>I can't do a simple <code>SUM(B)</code> because that would return <strong>50</strong>. I also cannot do <code>SUM(B) OVER (PARTITION BY A)</code> because that would do a running total by group, not by change of field. </p>
  
  <p>My output needs to look like this:</p>
  
  <pre><code>A    B    X
  123  5    5
  123  5    5
  456  10   15
  789  15   30
  789  15   30
  </code></pre>
  
  <p>I am trying to do this within a simple query. Is there a particular function I can use to do this? </p>