From: Richard Broersma Jr <rabroersma@yahoo.com>
To: Gerardo Herzig <gherzig@fmed.uba.ar>
To: pgsql-sql@postgresql.org
Subject: Re: union with count?
Date: Wed, 28 Mar 2007 07:27:29 -0700 (PDT)
Message-ID: <437650.83949.qm@web31809.mail.mud.yahoo.com> (raw)
In-Reply-To: <460A75ED.6070501@fmed.uba.ar>
--- Gerardo Herzig <gherzig@fmed.uba.ar> wrote:
> Hi dudes, i have the following question
> i have 2 tables with the same format, and i want to know if is there a
> possibility of using some sort of count(*) for achieving this result:
> select a from table1 where (condition) union select a from table2 where
> (condition), count(a) group by a
I think you are showing count(a) in the wrong spot in your example. But if I understand you
correctly the following should do what you want:
SELECT count(a)
FROM
(
SELECT a
FROM table1
WHERE (your_condition = true)
UNION -- you can use UNION ALL if you
-- want rows that are duplicates from each table.
SELECT a
FROM table2
WHERE (your_condition = true)
)
GROUP BY a;
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: rabroersma@yahoo.com, gherzig@fmed.uba.ar
Subject: Re: union with count?
In-Reply-To: <437650.83949.qm@web31809.mail.mud.yahoo.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