title: How to do a SUM() inside a case statement in SQL server
body: <p>I want to add some calculation inside my case statement to dynamically create the contents of a new column but I get the error: </p>
  
  <blockquote>
    <p><em>Column 'Test1.qrank' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.</em></p>
  </blockquote>
  
  <p>This is the code I'm working on</p>
  
  <pre><code>case 
      when test1.TotalType = 'Average' then Test2.avgscore
      when test1.TotalType = 'PercentOfTot' then (cnt/SUM(test1.qrank))
      else cnt
  end as displayscore
  </code></pre>
  
  <p>I did try to group but it didn't work.</p>
  
  <p>Any hints?</p>