Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 8167C475F0F for ; Tue, 30 Jul 2002 04:51:57 -0400 (EDT) Received: by news.hub.org (Postfix, from userid 8) id EA612381C23; Tue, 30 Jul 2002 04:51:31 -0400 (EDT) From: cnliou@eurosport.com (cnliou) X-Newsgroups: comp.databases.postgresql.sql Subject: Group And Sort After Union Date: 30 Jul 2002 01:46:22 -0700 Organization: http://groups.google.com/ Lines: 26 Message-ID: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-abuse@google.com To: pgsql-sql@postgresql.org X-Archive-Number: 200208/22 X-Sequence-Number: 8868 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