title: Invalid column name error while using group by
body: <p>I am getting Invalid column error on acctkey column. Can you please tell me what am I doing wrong?</p>
  
  <pre><code>select COUNT(acctkey) as Num, case
              when source_sys_cd in ('TRS','CLS') then source_sys_cd + '|'+[acct_num]                 
              when source_sys_cd = 'Hogan CIS' and [acct_type_cd] = 'DDA' then 'DDA' + '|'+ [acct_id]
              when source_sys_cd = 'Hogan CIS' and [acct_type_cd] != 'DDA' then 'TDA' +'|'+ [acct_id]
              when source_sys_cd = 'CLN' then source_sys_cd + '|'+ [acct_num]+ [acct_id]
              when source_sys_cd = 'RCC' then source_sys_cd + '|'+ [acct_id]
              when source_sys_cd = 'ITF' then source_sys_cd + '|'+ [acct_id]+ [acct_num]
              when source_sys_cd = 'SEC' then source_sys_cd + '|'+ [acct_id]
              else source_sys_cd + '|'+ [acct_num]
              end  as acctkey
              from mtb..STAGING_CUST_ACCT
              group by source_sys_cd
              ,acct_id
              ,acct_num
              ,acctkey
              ,acct_type_cd
  </code></pre>