Group And Sort After Union

From: cnliou(at)eurosport(dot)com (cnliou)
To: pgsql-sql(at)postgresql(dot)org
Subject: Group And Sort After Union
Date: 2002-07-30 08:46:22
Message-ID: eaa50a08.0207300046.fd57ade@posting.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rajesh Kumar Mallah. 2002-07-30 12:38:26 Is login_cnt is reserved attribute name?
Previous Message ROUWEZ Stephane 2002-07-30 08:07:24 Re: Change size of a field