title: Multiple having statements in a SQLAlchemy query
body: <p>What is the order of operations in SQLAlechemy?  An ideal answer will provide a set of generally applicable rules rather than a solution in the context of a specific example.</p>
  
  <p>The motivation for this question is as follows.  I am building a dataset from multiple tables within a SQLite3 database.  My query is something like the following:</p>
  
  <pre><code>query = session.query( Customer, Application, Income, Spending ).\
  join(
      &lt;USER-APPLICATION JOIN&gt;
  ).\
  join(
      &lt;USER-INCOME JOIN&gt;
  ).\
  join(
      &lt;USER-SPENDING JOIN&gt;
  ).\
  group_by(
      User.id,
      Application.date,
  )
  </code></pre>
  
  <p>Now, I don't want to pull everything from these tables, so I put some filters on the query.  For example:</p>
  
  <pre><code>query = query.\
  filter( 
      Income.date &lt;= Application.date
  ).\
  having(
      or_(
          and_(
              func.max(Spending.date) &lt;= Application.date,
              Spending.date == func.max(Spending.date)
          ),
          and_(
              func.max(Spending.date) &gt; Application.date,
              Spending.date &lt; Application.date
          ),
          and_(
              func.min(Spending.date) &gt; Application.date,
              Spending.date == func.min(Spending.date)
          )
      )
  ).\
  having(
      Spending.date == func.max(Spending.date),
      Income.date == func.max(Income.date)
  )
  </code></pre>
  
  <p>However, the having statements are dependent upon one-another, and the code above will do what I want if and only if the first having statement is applied before the second.  </p>
  
  <p>So is this code behaving in the way I want?  Also, does this behavior depend on which database I use (I may want to upgrade databases in the future)?</p>