title: Sql query to add consecutive row values until a condition is met
body: <p>I have a small SQL table as follows:</p>
  <pre class="lang-sql prettyprint-override"><code>    create table temp (
          company varchar2(10) not null,
          sales NUMBER,
          parent_b VARCHAR2(10),
          PRIMARY KEY (company),
          CONSTRAINT company_branch_reference FOREIGN KEY (parent_b)
          REFERENCES temp(company)
      );
  </code></pre>
  <p>The contents of the table are as follows:</p>
  <pre><code>+---------+-------+----------+
  | COMPANY | SALES | PARENT_B |
  +---------+-------+----------+
  | A1      |    20 | A1       |
  | A2      |    30 | A1       |
  | A3      |    30 | A1       |
  | A4      |    40 | A2       |
  | A5      |    10 | A2       |
  | A6      |    20 | A3       |
  | B1      |    40 | B1       |
  | B2      |    20 | B1       |
  +---------+-------+----------+
  </code></pre>
  <p>In this case, each company has a reference to the parent company.
  For example, <strong>A1</strong> itself is a parent so the <strong>Parent</strong> column has <strong>A1</strong>
  for <strong>A2, A3</strong> the <strong>Parent</strong> column has <strong>A1</strong></p>
  <p>I want to calculate the total sales of each parent as follows:<br />
  The parent <strong>A1</strong> has a total sale of 20+30+30+40+10+20 = <strong>150</strong><br />
  The parent <strong>B1</strong> has a total sale of 40+20 = <strong>60</strong></p>
  <p>The output should be as follows:</p>
  <pre><code>+----------+-------+
  | Parent_b | Sales |
  +----------+-------+
  | A1       |   150 |
  | B1       |    60 |
  +----------+-------+
  </code></pre>
  <p>Can someone please help me out with a MySQL query for this scenario.</p>
  <p>Thanks in advance!</p>