Re: union with count?

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: Gerardo Herzig <gherzig(at)fmed(dot)uba(dot)ar>, pgsql-sql(at)postgresql(dot)org
Subject: Re: union with count?
Date: 2007-03-28 14:27:29
Message-ID: 437650.83949.qm@web31809.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


--- Gerardo Herzig <gherzig(at)fmed(dot)uba(dot)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;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Gerardo Herzig 2007-03-28 15:23:17 Re: union with count?
Previous Message A. Kretschmer 2007-03-28 14:24:44 Re: union with count?