title: SQL Count of Occurence
body: <p>I have these two tables A and B</p>
  
  <p>A:</p>
  
  <pre><code>id|name
  1 | AAA
  2 | BBB
  3 | CCC
  </code></pre>
  
  <p>B:</p>
  
  <pre><code>id|a_ref_id|name2
  1 |1       |aaa
  2 |1       |aAa
  3 |2       |bbb
  4 |3       |ccc
  5 |3       |cCc
  </code></pre>
  
  <p>This tables can be joined via <code>a_ref_id</code>.</p>
  
  <p>I want to have a output like this:</p>
  
  <pre><code>A_id|A_name|B_id|B_name2|A_id_occurencse_in_this_query
  1   |AAA   |1   |aaa    |1
  1   |AAA   |2   |aAa    |2
  2   |BBB   |3   |bbb    |1
  3   |CCC   |4   |ccc    |1
  3   |CCC   |5   |cCc    |2
  </code></pre>
  
  <p>The last column is my problem.</p>
  
  <p>The Join is easy going but i have no idea how to get the last Column. I had tried with <code>COUNT</code>, <code>GROUP BY</code>, <code>HAVING</code>, etc. but no luck.</p>
  
  <p>Any Idea's?</p>