Use where() when applying conditions on calculated fields

This is to address the issue when having() was using without
the field expression and causing the error about undefined
fields. This may also improve performance in SQL.
Merge branch 'calculated_where'