title: SQL find first value greater than X in a sorted column
body: <p>Given a table like this</p>
  
  <pre><code>id      total_cost  margin
  a       2           10%
  b       4           15%
  c       6           4%
  x       7           90%
  y       8           13%
  z       9           0%
  </code></pre>
  
  <p>Where the total cost is defined as a running aggregate of some positive column, so it's always sorted. 
  I need to find out in a single SQL command (needs to be efficient) the average margin where cost first exceeds or equals a number X.</p>
  
  <p>i.e. given X = 7.5</p>
  
  <p>I need to find the average margin where total_cost first exceeds or equal to 7.5. In this case the condition would be applied to the first 5 columns since </p>
  
  <pre><code>id      total_cost  margin
  y       8           13%
  </code></pre>
  
  <p>is the first column where total_cost exceeds 7.5. The result would be</p>
  
  <pre><code>avg(10%, 15%, 4%, 90%, 13%)
  </code></pre>