title: How Do I Use Case Statement Column In Group By
body: <p>As stated by the question, I'm trying to formulate a query that has a case statement in the column results, and then I want to include that column in the query's group by statement. To give a concrete example, here is all little of what my query looks like:</p>
  
  <pre><code>SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
  CASE
      WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
      ELSE 'B'
  END AS CASE_COLUMN
  FROM SOME_TABLE SOME_TABLE_ALIAS
  ... (other table joins and where clauses)
  GROUP BY SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2, CASE_COLUMN
  </code></pre>
  
  <p>Before coming here, I checked out a few websites, including <a href="http://www.tek-tips.com/viewthread.cfm?qid=1116474" rel="noreferrer">this one</a>, to try solve my problem. I've tried adding another alias after the <code>CASE</code> keyword like is shown in the linked web page but have had no luck. The error message I continue to receive is the following:</p>
  
  <pre><code>[Error] Script lines: 127-151 ----------------------
   CASE_COLUMN IS NOT VALID IN THE CONTEXT WHERE IT IS USED. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.53.71 
  </code></pre>
  
  <p>Has anyone else run into the issues I'm facing and been able to use a <code>GROUP BY</code> on the results of a <code>CASE</code> statement? Any help would be appreciated. Oh, and the DB2 version is a z/OS instance, version 10 (DSN10015)</p>