title: calculating percentage in postgresql with conditions
body: <p><strong>I have one table and I want to calculate the percentage of one column</strong>
  I tried to do so in two ways. 
  but I am actually face with error.</p>
  
  <p>The error is 'syntax error at or near "select"' </p>
  
  <p>This is my code in below:</p>
  
  <pre><code>WITH total AS 
  (
      select krs_name,count(fclass) as cluster
      FROM residentioal
      GROUP BY krs_name
  )
  SELECT total.cluster, 
         total.krs_name
         (select count(fclass)
          FROM   residentioal
          where  fclass='village' 
          or     fclass='hamlet' 
          or     fclass='suburb' 
          or     fclass='island'
          AND    krs_name = total.krs_name
         )::float / count(fclass) * 100 as percentageofonline
   FROM  residentioal, total 
   WHERE residentioal.krs_name = total.krs_name
   GROUP BY total.krs_name total.krs_name
  </code></pre>
  
  <p>My table has 5437 rows in which there is 8 group of krs_name and in the other column namely fclass, there is 6 group. Therefore I want to calculate the percentage of 4 groups from fclass for each krs_name . thus, i have to first query the count(fclass) group by krs_name  and then query the count of fclass where fclass is equal to my condition group by krs_name and finally count(fclass) "with condition" / count(fclass) "total fclass" * 100 goup by krs_name?</p>
  
  <p>I'm using Postgresql 9.1.</p>