pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Terry Yapt <yapt@technovell.com>
To: cnliou <cnliou@eurosport.com>
To: pgsql-sql@postgresql.org
Subject: Re: Group And Sort After Union
Date: Mon, 05 Aug 2002 11:55:49 +0200
Message-ID: <3D4E4BA5.3689A1EA@technovell.com> (raw)
References: <eaa50a08.0207300046.fd57ade@posting.google.com>

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



view thread (3+ messages)

Message-ID: <3D4E4BA5.3689A1EA@technovell.com>
Permalink:  ../3D4E4BA5.3689A1EA@technovell.com/
Also on:    postgresql.org/message-id/3D4E4BA5.3689A1EA@technovell.com

 · 

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: yapt@technovell.com, cnliou@eurosport.com
  Subject: Re: Group And Sort After Union
  In-Reply-To: <3D4E4BA5.3689A1EA@technovell.com>

* 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