title: SQL server: Invalid column name error
body: <p>Here is my sql script, I want to set the aggregation method dynamically by using CASE:  </p>
  
  <pre><code>select t.tagname,
    case t.aggregationmethod 
    when 'Sum' then SUM 
    when 'Average' then AVG 
    WHEN 'Max' then MAX 
    WHEN 'Min' THEN MIN ELSE SUM END +'('+tv.Value+')'
    from tag t, tagvalue tv where t.tagid=tv.tagid
  </code></pre>
  
  <p>when executed the script, it will pop up an error message: </p>
  
  <pre><code>    Invalid column name 'SUM'.
      Invalid column name 'AVG'.
      Invalid column name 'MAX'.
      Invalid column name 'MIN'.
  </code></pre>
  
  <p>My questions:<br>
  (1) how to fix my above script, where the problem is ?<br>
  (2) Is there other way to set aggregation method dynamically?</p>