Re: Group And Sort After Union

From: Terry Yapt <yapt(at)technovell(dot)com>
To: cnliou <cnliou(at)eurosport(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Group And Sort After Union
Date: 2002-08-05 09:55:49
Message-ID: 3D4E4BA5.3689A1EA@technovell.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Robert Treat 2002-08-05 21:40:44 Re: VACUUM not doing its job?
Previous Message Jochem van Dieten 2002-08-04 11:22:27 Re: VACUUM not doing its job?