use subquery instead of WITH for psql 9.0 and before