title: How do I get the total number of results returned from a subquery in Postgresql?
body: <p>I am trying to get the total number of results returned from a subquery. This is my query:</p>
  
  <pre><code>select 
      count(r.reason_id) as num,
      cast (count(r.reason_id) as float) / (select count(*) as total from r) * 100.0 as pct
  from (
      select
          case
              when rarreason != 0 then rarreason
              else rejectreason end as reason_id
      from 
          workorderlines 
      where
          (rarreason != 0 or rejectreason != 0)
      ) as r
  
  group by
      r.reason_id
  </code></pre>
  
  <p>However, when I try and execute this, I get this error:</p>
  
  <pre><code>ERROR:  relation "r" does not exist
  LINE 3: ...on_id) as float) / (select count(*) as total from r) * 100.0...
                                                               ^
  
  ********** Error **********
  
  ERROR: relation "r" does not exist
  SQL state: 42P01
  Character: 112
  </code></pre>
  
  <p>How do I do this? I am using Postgresql 9.1. Thanks!</p>