Move the predicate from the WHERE
clause to a HAVING
clause, and you can reference field2 by it's alias:
In that first query, just change WHERE
to HAVING
. That will work.
(The HAVING
clause operates on the resultset after it's prepared; the WHERE
clause is evaluated much earlier in the processing, that determines which rows get included in the resultset, and can filter out a lot of rows, which makes things more efficient.
NOTE: your query may not be the most efficient approach to getting the resultset you want; but there's not enough information to make a recommendation.