pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Group And Sort After Union
3+ messages / 3 participants
[nested] [flat]

* Group And Sort After Union
@ 2002-07-30 08:46 cnliou <cnliou@eurosport.com>
  2002-08-02 13:47 ` Re: Group And Sort After Union Tom Lane <tgl@sss.pgh.pa.us>
  2002-08-05 09:55 ` Re: Group And Sort After Union Terry Yapt <yapt@technovell.com>
  0 siblings, 2 replies; 3+ messages in thread

From: cnliou@eurosport.com @ 2002-07-30 08:46 UTC (permalink / raw)
  To: 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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Group And Sort After Union
  2002-07-30 08:46 Group And Sort After Union cnliou <cnliou@eurosport.com>
@ 2002-08-02 13:47 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2002-08-02 13:47 UTC (permalink / raw)
  To: cnliou@eurosport.com; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Group And Sort After Union
  2002-07-30 08:46 Group And Sort After Union cnliou <cnliou@eurosport.com>
@ 2002-08-05 09:55 ` Terry Yapt <yapt@technovell.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Terry Yapt @ 2002-08-05 09:55 UTC (permalink / raw)
  To: cnliou <cnliou@eurosport.com>; 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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2002-08-05 09:55 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-07-30 08:46 Group And Sort After Union cnliou <cnliou@eurosport.com>
2002-08-02 13:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-08-05 09:55 ` Terry Yapt <yapt@technovell.com>

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