Brandon Metcalf <brandon@geronimoalloys.com> writes:
> I tried moving the last group of WHERE, GROUP BY, and ORDER BY before
> the UNION with the query it belongs to, but that results in a
> different syntax error.
I think that's probably what you want to do. What you're missing is
you need parentheses to put an ORDER BY into an arm of a UNION:
(SELECT ... ORDER BY ...) UNION SELECT ...
Otherwise it wants to consider the ORDER BY as applying to the UNION
output.
regards, tom lane