Fix order by handling for subqueries

Related to #1093