pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: cnliou@eurosport.com
Cc: pgsql-sql@postgresql.org
Subject: Re: Group And Sort After Union
Date: Fri, 02 Aug 2002 09:47:20 -0400
Message-ID: <29427.1028296040@sss.pgh.pa.us> (raw)
In-Reply-To: <eaa50a08.0207300046.fd57ade@posting.google.com>
References: <eaa50a08.0207300046.fd57ade@posting.google.com>

cnliou@eurosport.com (cnliou) writes:
> 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;

Correct is

SELECT * FROM
(SELECT c11 AS c1,SUM(c12) AS c2 FROM table1
UNION
SELECT c21 AS c1,SUM(c22) AS c2 FROM table2
) ss
GROUP BY c1
ORDER BY c2;

			regards, tom lane



view thread (3+ messages)  latest in thread

Message-ID: <29427.1028296040@sss.pgh.pa.us>
Permalink:  ../29427.1028296040@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/29427.1028296040@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, cnliou@eurosport.com
  Subject: Re: Group And Sort After Union
  In-Reply-To: <29427.1028296040@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox