Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 88B88475481 for ; Mon, 5 Aug 2002 05:55:40 -0400 (EDT) Received: from technovell.com (213-97-10-232.uc.nombres.ttd.es [213.97.10.232]) by postgresql.org (Postfix) with ESMTP id A4F8947514C for ; Mon, 5 Aug 2002 05:55:39 -0400 (EDT) Received: from technovell.com yapt@technovell.com [195.55.249.227] by technovell.com with Novell NIMS $Revision: 2.88.1.1 $ on Linux; Mon, 05 Aug 2002 12:10:50 +0100 Message-ID: <3D4E4BA5.3689A1EA@technovell.com> Date: Mon, 05 Aug 2002 11:55:49 +0200 From: Terry Yapt X-Mailer: Mozilla 4.79 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: cnliou , pgsql-sql@postgresql.org Subject: Re: Group And Sort After Union References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/78 X-Sequence-Number: 8924 Perhaps... SELECT t.c1, sum(t.c2) FROM ( SELECT c11 AS c1, SUM(c12) AS c2 FROM table1 GROUP BY c11 UNION SELECT c21 AS c1, SUM(c22) AS c2 FROM table2 GROUP BY c21 ) AS t GROUP BY t.c1 ORDER BY sum(t.c2); I hope this helps, or at least, give you a clue. Best Regards.... I have not tested it... please cnliou wrote: > > Greetings! > > I want to GROUP BY and ORDER BY on the result of UNION similar to the > following (wrong) SQL: > > (SELECT c11 AS c1,SUM(c12) AS c2 FROM table1 > UNION > SELECT c21 AS c1,SUM(c22) AS c2 FROM table2 > ) > GROUP BY c1 > ORDER BY c2; > > Please note that the following is NOT what I want because it generates > 2 groups of data set: > > SELECT c11 AS c1,SUM(c12) AS c2 FROM table1 > GROUP BY c1 > ORDER BY c2 > UNION > SELECT c21 AS c1,SUM(c22) AS c2 FROM table2 > GROUP BY c1 > ORDER BY c2; > > How do I do that? Thank you in advance! > > CNLIOU > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org