Re: Having the sum of two queries

From: Hélder M(dot) Vieira <hmv(at)mail(dot)telepac(dot)pt>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Having the sum of two queries
Date: 2007-07-16 22:00:47
Message-ID: 001401c7c7f4$c4515280$6200a8c0@hmv02
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>... SELECT tot.status, COUNT(total)
> FROM( QUERY A UNION QUERY B ) AS tot
> GROUP BY tot.status
>
> But it doesn't works. It doesn't add the total columns with the same
> status...
>...

I guess you have two problems, if I understand what you're trying to do.
- As Roberto Spier noted, 'sum' would be better than 'count' in the outer
query.
- Also, 'union' will silently remove duplicate rows, unless you use 'union
all'.

Please check if this is what you want:

select status, count(status)
from
(
select status from users
union all
select status from imported_users
) as fff
group by status;

Regards,
´
Helder M. Vieira

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jyoti Seth 2007-07-17 05:18:41 Re: [OBORONA-SPAM] calling webservice through postgresql function
Previous Message Dmitry Turin 2007-07-16 13:18:57 Re: [OBORONA-SPAM] calling webservice through postgresql function