title: generate a current,30,60,90 day aging report with mysql
body: <p>I am trying to get aging of invoices report from a mysql table. The table has a client_id, an amount_due, and a due_date for each invoice.  I am grabbing all the invoices from each client, one client at a time. </p>
  
  <p>I have searched SO for a solution and found several but when I try to implement them, I get the same error ... </p>
  
  <blockquote>
    <p>"unknown column '<code>days_past_due</code>"....</p>
  </blockquote>
  
  <p>I have tried several snippets of mysql code and each one errors the same way, I researched the error and it seems I can't use a column alias at the same level(don't quite understand that).  I tried this first. (NOTE: INV_DDATE is stored as a unix timestamp, an 11 digit integer)</p>
  
  <pre><code>select INV_NUM
       , INV_DDATE
       , DATEDIFF(CURDATE(), FROM_UNIXTIME(INV_DDATE)) days_past_due
       , INV_DBAL total_ar
       , SUM(IF(days_past_due = 0, total_ar, 0))
       , SUM(IF(days_past_due BETWEEN 1 AND 30, total_ar, 0))
       , SUM(IF(days_past_due BETWEEN 31 AND 60, total_ar, 0))
       , SUM(IF(days_past_due BETWEEN 61 AND 90, total_ar, 0))
       , SUM(IF(days_past_due &gt; 90, total_ar, 0)) 
    from invoice_table 
   where INV_CODE = 'the_client code'
  </code></pre>
  
  <blockquote>
    <h1>1054 - Unknown column 'days_past_due' in 'field list'.</h1>
  </blockquote>