title: mysql address outer query from subquery
body: <p>I have this query:</p>
  <pre><code>SELECT 
      sec_to_time(avg(t1.sessiontime)) as aloc,
      CONCAT(TRUNCATE(sum(t1.terminatecauseid = 1) * 100 / count(*),
                  1),
              '%') as asr,
      count(*) as calls,
      cast(t1.destination as unsigned) as prefix,
      t2.destination as destination,
      SEC_TO_TIME(sum(t1.sessiontime)) as duration
  FROM
      cc_call AS t1
          inner join
      cc_prefix as t2 ON t1.destination = t2.prefix
  WHERE
      t1.card_id = '133' AND t1.starttime &gt;= ('2014-06-1') AND t1.starttime &lt;= ('2014-07-01 23:59:59') and t1.terminatecauseid = 1
  group by t1.destination
  order by duration DESC
  LIMIT 0 , 25
  </code></pre>
  <p>t1.terminatecauseid = 1 means successful call,
  'asr' means average success rate,</p>
  <p>Im trying to find out how many calls with (t1.terminatecauseid = 1) from the total calls made to an extension.</p>
  <p>this line doesn't work:</p>
  <pre><code>sum(t1.terminatecauseid = 1) * 100 / count(*)
  </code></pre>
  <p>since I already have (t1.terminatecauseid = 1) in the WHERE clause.</p>
  <p>Im thinking about putting a subquery, to retrieve total calls, where count(*) currently is.</p>
  <p>How can I have this query calculate the ASR with total calls made?</p>
  <p>example <a href="http://www.sqlfiddle.com/#!2/e4e2f3/20" rel="nofollow noreferrer">sqlfiddle</a></p>
  <p>if possible, I'd like to <em>not</em> show results with duration=NULL</p>