Paefgen, Peter (LDS) wrote:
Hello,
a) it is right, to place an ORDER BY at the end of the statement. That is the
correct place to do this.
Correct - the ORDER BY at the end of the statement will apply to the
result set after the UNION has been done.
ORDER BY can be included in the sub-expression SELECT statements only if
they are enclosed in parenthesis and I believe it *must* also be
accompanied by a LIMIT clause.
but
b) UNION doesn't mean anyway an sort. UNION mean "eliminate dups". But there are
UNION means to join/combine together and if used on it's own will
eliminate duplicate rows in the result set.
Using UNION ALL will include any duplicates and run somewhat faster.
UNION itself does not imply any sort order but ORDER BY can be applied
to the result set of the UNION.
several techniques to do that. One of them is "sort". So, if the development
crew chooses to change the elimination algorithme, that would be, in sight of an
UNION, ok .
As long as the columns returned match up to satisfy the UNION criteria
then changing the elimination algorithm will not break your use of UNION.
If they want to use ORDER BY x LIMIT 2 then that sub-select will need to
be surrounded by parenthesis.
Best wishes,
Regards,
Peter
--
Shane Ambler
pgSQL@xxxxxxxxxxxxxxxx
Get Sheeky @ http://Sheeky.Biz