title: Select Average of Top 25% of Values in SQL
body: <p>I'm currently writing a stored procedure for my client to populate some tables that will be used to generate SSRS reports later on. Some of the data is based on specific stock formulas that are run on each of their clients' quarterly data (sent to them by their clients). The other part of the data is generated by comparing those results against those from other, similar sized clients. One of the things that they want tracked in their reports is the average of the top 25% of formula results for that particular comparison group.</p>
  
  <p>To give a better picture of it, imagine the following fields that I have in a temp table:</p>
  
  <pre><code>FormulaID int
  Value decimal (18,6)
  </code></pre>
  
  <p>I want to do the following: Given a specific <code>FormulaID</code> return the average of the top 25% of <code>Value</code>.</p>
  
  <p>I know how to take an average in SQL, but I don't know how to do it against only the top 25% of a specific group.</p>
  
  <p>How would I write this query?</p>