title: Grouping by id in across columns
body: <p>I have a table with the following columns</p>
  
  <pre><code>timetrack_id | timetrack_length | timetrack_rate | timetrack_parent
  </code></pre>
  
  <p>the <code>timetrack_parent</code> holds a <code>timetrack_id</code> if the time tracking has been continued, but will be <code>0</code> if it has not.</p>
  
  <p>I would like to pull back a single row for each timetrack_id, but combine the <code>timetrack_length</code> where <code>timetrack_parent = timetrack_id</code>. Then the rest of the values should be from the parent row (where timetrack_parent = 0 and rows exists with a timetrack_parent of this timetrack_id).</p>
  
  <p>So for the following data</p>
  
  <pre><code>timetrack_id | timetrack_length | timetrack_rate | timetrack_parent
  -------------+------------------+----------------+-------------------
  1            | 5                | 20.00          | 0
  2            | 7                | 20.00          | 0
  3            | 8                | 20.00          | 0
  4            | 20               | 20.00          | 1
  5            | 15               | 25.00          | 1
  6            | 6                | 30.00          | 3
  </code></pre>
  
  <p>I would return</p>
  
  <pre><code>1 | 40 | 20.00 | 0
  2 | 7  | 20.00 | 0
  3 | 14 | 20.00 | 0
  </code></pre>
  
  <p>I have the following query</p>
  
  <pre><code>SELECT `timetrack_id`, `timetrack_parent`, `timetrack_rate`, SUM(`timetrack_length`) 
  FROM `timetrack` 
  GROUP BY `timetrack_parent`, `timetrack_parent` = `timetrack_id`
  </code></pre>
  
  <p>This does Group the rows by <code>timetrack_parent</code> and <code>SUM</code> the <code>timetrack_length</code>, but this also groups those that have a <code>timetrack_parent</code> of 0, which are not related. This query also doesn't group where <code>timetrack_parent = timetrack_id</code>. I'm also not sure the other data from the <em>parent</em> row will always be returned. </p>
  
  <p>So what is returned is</p>
  
  <pre><code>1 | 20 | 20.00 | 0
  4 | 35 | 20.00 | 1
  6 | 6  | 30.00 | 3
  </code></pre>
  
  <p>Can someone offer some direction please?</p>