Received: from localhost (uranus.hub.org [200.46.204.60]) by postgresql.org (Postfix) with ESMTP id 73A399FA333 for ; Tue, 21 Nov 2006 11:45:20 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.60]) (amavisd-new, port 10024) with ESMTP id 59796-06 for ; Tue, 21 Nov 2006 11:45:17 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from ipmail02.adl2.internode.on.net (ipmail02.adl2.internode.on.net [203.16.214.141]) by postgresql.org (Postfix) with ESMTP id 923EA9FA2B9 for ; Tue, 21 Nov 2006 11:45:06 -0400 (AST) Received: from ppp247-71.static.internode.on.net (HELO [192.168.8.200]) ([203.122.247.71]) by ipmail02.adl2.internode.on.net with ESMTP; 22 Nov 2006 02:15:02 +1030 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAEKuYkXLevdH/2dsb2JhbAANHg X-IronPort-AV: i="4.09,443,1157293800"; d="scan'208"; a="49320907:sNHT75007793" Message-ID: <45631EFD.8050106@007Marketing.com> Date: Wed, 22 Nov 2006 02:15:01 +1030 From: Shane Ambler User-Agent: Thunderbird 1.5.0.7 (Macintosh/20060909) MIME-Version: 1.0 To: "Paefgen, Peter (LDS)" CC: Chris Mulcahy , pgsql-general@postgresql.org Subject: Re: ORDER BY References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.86 tagged_above=0 required=5 tests=AWL, BAYES_40, BIZ_TLD X-Spam-Level: X-Archive-Number: 200611/1177 X-Sequence-Number: 104273 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@007Marketing.com Get Sheeky @ http://Sheeky.Biz