title: SQL - Finding percentage population between 0 and population average
body: <p>I am trying to find the percentage of population for each grouped row between 0 and the average of that group. For example, in the below query, say I have a row where num_problems is 100 and average is 70. There are 58 values in the population (for that row) which have values below 70. I want to extract the value (58) as a part of the result tupule.</p>
  
  <pre><code>select
      tm.subject_name,
      tm.topic_name,
      pm.problem_type,
      count( pa.id ) as num_problems,
      avg( pa.duration ) as average ,
      stddev( pa.duration )
  from
      problem_attempt pa,
      problem_master pm,
      topic_master tm
  where
      pa.problem_id = pm.id and
      pm.topic_id = tm.id and
      pa.outcome = 'Solved' and
      pa.duration &gt; 10 and 
      pa.duration &lt; 1000 and
      pm.problem_type = 'SCA'
  group by
      tm.subject_name,
      tm.topic_name,
      pm.problem_type ;
  </code></pre>