title: DB2 alias in WHERE clause
body: <p>I have a couple DB2 tables, one for <code>users</code> and one for <code>newsletters</code> and I want to select using an alias in the <code>WHERE</code> clause.</p>
  
  <pre><code>SELECT a.*, b.tech_id as user FROM users a  
  JOIN newsletter b ON b.tech_id = a.newsletter_id
  WHERE timestamp(user) &lt; current_timestamp
  </code></pre>
  
  <p>This is radically simplified so I can see what's going on, but I am getting an error that makes me think that the <code>user</code> alias isn't getting passed correctly: </p>
  
  <pre><code>ERROR: An invalid datetime format was detected; that is, an 
  invalid string representation or value was specified. 
  </code></pre>
  
  <p>The <code>user.tech_id</code> is a string built from the <code>datetime</code> when the record was created, so it looks something like <code>20150210175040951186000000</code>. I've verified that I can execute a <code>timestamp(tech_id)</code> successfully-- so it can't be the format of the field causing the problem.</p>
  
  <p>Any ideas?</p>
  
  <p>More information:</p>
  
  <p>There's multiple newsletters per user. I need to get the most recent newsletter (by the tech_id) and check if that was created in the past week. So the more complex version would be something like:</p>
  
  <pre><code>SELECT a.*, b.tech_id as user FROM users a  
  JOIN newsletter b ON b.tech_id = a.newsletter_id
  WHERE timestamp(max(user)) &lt; current_timestamp
  </code></pre>
  
  <p>Is there a way to <code>JOIN</code> only on the most recent record?</p>