title: Using datediff in MySQL statement
body: <p>This is my SQL statement that works using datediff:</p>
  
  <pre><code>SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate 
  FROM  B134HREC 
  WHERE datediff (dd, '2006-05-05', getdate()) &gt; 90
  GROUP BY b134_rmcid, b134_recdate 
  ORDER BY b134_recdate DESC, b134_rmcid
  </code></pre>
  
  <p>I need to Replace the Hardcoded date '2006-05-05' with MAX(b134_recdate) like so:</p>
  
  <pre><code>SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate 
  FROM  B134HREC 
  where datediff (dd, MAX(b134_recdate), getdate()) &gt; 90
  GROUP BY b134_rmcid, b134_recdate 
  ORDER BY b134_recdate DESC, b134_rmcid
  </code></pre>
  
  <p>But i get this Error message:</p>
  
  <blockquote>
    <p>An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.</p>
  </blockquote>
  
  <p>Any idea how to fix my SQL Statement?</p>