title: How to iterate this SQL Query?
body: <p>I've got this query: </p>
  
  <pre><code>SELECT TOP 1 A.ExamTemplateId, A.Id AS AnsweredTestId, AVG(B.Score) AS AvgScore, A.[Date]
  FROM AnsweredTest AS A
  INNER JOIN AnsweredWorksheet AS B ON (A.Id = B.AnsweredTestId)
  WHERE A.StudentId = 'OPA-3DKCL2' AND A.ExamTemplateId = 1
  GROUP BY A.Id, A.ExamTemplateId, A.[Date]
  ORDER BY AvgScore DESC
  </code></pre>
  
  <p>Please check that I have a <code>WHERE</code> condition which receives a static number. The query result is:</p>
  
  <p>EXAMPLE 1
  <img src="https://i.stack.imgur.com/EZqHP.png" alt=""></p>
  
  <p>EXAMPLE 2
  <img src="https://i.stack.imgur.com/mAdO8.png" alt="enter image description here"></p>
  
  <p>And it is correct! But now, I'd like to show all the records from a ExamTemplate table. But now I don't know how can I accomplish that. I was thinking creating a function but I'm not sure, it's just an idea.</p>
  
  <p>The final output which I'd like to receive is the union of last records:</p>
  
  <pre><code>1   1   0.7 2012-10-21 19:50:11.697
  2   10  0.555555555555556   2012-10-22 18:58:24.163
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p>UPDATE:</p>
  
  <p>I've got highlighted with yellow color the rows which I'm interested. I set in the last query <code>TOP 1</code> because I want to get the highest average score by <code>ExamTemplateId</code>. And that's will be all.</p>
  
  <p><img src="https://i.stack.imgur.com/VUALu.png" alt="enter image description here"></p>