how to aggregate with case function
<p>I have some code that works but I need to calculate some dta and cannot seem to make it work. </p>

<p>See the code below and and the on with the comment of does not work.</p>

<pre><code> SELECT 
   siteid,
   linenum,
   worktype,
   COUNT(CASE WHEN status = 'APPR' THEN 1 ELSE NULL END)   AS [Approved],
   COUNT(CASE WHEN status = 'review' THEN 1 ELSE NULL END)   AS [Review],
   COUNT(CASE WHEN status = 'wmatl' THEN 1 ELSE NULL END)   AS [WaitMatl],
   COUNT(CASE WHEN status = 'comp' THEN 1 ELSE NULL END)   AS [Complete],
   COUNT(CASE WHEN status = 'incomp' THEN 1 ELSE NULL END)   AS [InComplete],
   COUNT(CASE WHEN status = 'closed' THEN 1 ELSE NULL END)   AS [Closed],
   COUNT(CASE WHEN status not in ('appr','wmatl') THEN 1 ELSE NULL END) AS [All_Completed],
   Count (*) as allrecords,
   /* The below divide by does not work */
   COUNT(CASE WHEN status not in ('appr','wmatl') THEN 1 ELSE null END)  / Count (*) as Completion_Ratio

  FROM workorder
  WHERE (siteid in ('p202','p203','p201')) AND (worktype in   ('mpm','ppm','tspm')) AND (istask ='0') 
AND (historyflag ='0')  AND (woclass = 'workorder') --AND (status not in ('comp','closed','review','incomp'))
AND (assetnum is not null) AND (maintby not in ('ms','ed')) 
AND targcompdate &lt; DATEADD(mm,DATEDIFF(mm,0,GETDATE())-0,0)
GROUP BY siteid,linenum,worktype
Order by siteid, linenum,worktype
</code></pre>