title: SQL parent/child sum values of final childs
body: <p>This is my plain table</p>
  
  <pre><code>PARENT CHILD QTY
  -----------------
  A      B     1
  A      C     2
  A      D     1
  B      E     1
  B      F     1
  E      F     1
  A      E     1
  </code></pre>
  
  <p>The expected output is looking like that</p>
  
  <pre><code>CHILD    SUM(QTY)
  C        2
  D        1
  F        6
  </code></pre>
  
  <p>Query should be summing all the quantity of "last childs" that means if A parent have B child and B is parent that means hierarchy </p>
  
  <pre><code>Aparent -&gt; Bchild -&gt; Bparent 
  </code></pre>
  
  <p>then F is last CHILD (no parent) and Echild of <code>B -&gt; Eparent -&gt; Fchild + 1</code>. So this is like F QTY1 2times from this disintegration.</p>
  
  <p>Summing whole plain table means C QTY 2, D QTY 1, and F QTY 6times 1.</p>
  
  <p>I found lot of similar questions, but this thinking is so complex for me that it is hard to understand.</p>
  
  <p>This is some combination of hierarchy and recursive. Thanks for your ideas</p>
  
  <p>EDIT logic explanation(C and D is just for essence here):  </p>
  
  <p>C QTY is get by A->C = QTY 2<br>
  D QTY is get by A->D = QTY 1  </p>
  
  <p>then  </p>
  
  <p><code>A -&gt; Bchild -&gt; Bparent</code> which split into:  </p>
  
  <p>Echild -> Eparent -> Fchild = QTY 1 +<br>
  Fchild = QTY 1 (it is 2 so far, then the same disintegration makes F qty 6, the logic is - IF CHILD is in PARENT then count CHILD QTY,but if CHILD is in PARENT then count IT'S CHILD until no Parent named CHILD is found.</p>
  
  <p>EDIT: See SQLFiddle<br>
  <a href="http://sqlfiddle.com/#!3/f27eef/1" rel="nofollow">SQLFIDDLE</a><br>
  This is query to get the expected result but w/o the recursive part...</p>