title: How to Group By on Static Data having UNION in SQL Server
body: <p>I am trying to do a <code>SELECT</code> with <code>UNION</code> in Microsoft SQL Server where SQL statement has static string columns on which I would like to do a <code>Group By</code>. I am getting this error:</p>
  <blockquote>
  <p>[Code: 207, SQL State: S0001] Invalid column name 'chrg_tx'. [Script position: 221 - 237]</p>
  </blockquote>
  <p>The same SQL works in other RDBMS like INFORMIX.</p>
  <p>Here is my SQL</p>
  <pre><code>SELECT 
      '' AS chrg_tx, 
      SUM(gross_after_discount) gross, 
      SUM(tax) tax, 
      SUM(net) net
  FROM 
      invce_line_item 
  WHERE 
      ctry_cd = 'US' 
      AND lcl_btch_id = '2021-11-1214:49:20.838.EN'
      AND invce_no = 'INV000001' 
      AND tax != 0 
      AND insr = 0 
  GROUP BY 
      chrg_tx
  
  UNION
  
  SELECT 
      'ASSURANCE' AS chrg_tx,
      SUM(insr + insr_dsct) gross,
      SUM(insr_tax) tax, 
      SUM(net_insr) net
  FROM 
      invce_line_item 
  WHERE 
      ctry_cd = 'US' 
      AND lcl_btch_id = '2021-11-1214:49:20.838.EN'
      AND invce_no = 'INV000001' 
      AND insr != 0 
  GROUP BY 
      chrg_tx
  </code></pre>
  <p>What can I try to resolve this?</p>