From: Tomasz Myrta <jasiek@klaster.net>
To: pgsql-sql@postgresql.org
Subject: sub-select with aggregate
Date: Wed, 23 Oct 2002 12:07:09 +0200
Message-ID: <3DB674CD.50404@klaster.net> (raw)
Hi
I want to perform query looking like this:
select
user_id,
a/sum_a as percent_a,
b/sum_b as percent_b
from
users join
(select
group_id,
sum(a) as sum_a,
sum(b) as sum_b
from users group by group_id) X using (group_id)
where group_id=3;
This query works, but very slow. Subquery with aggregate is performed
for all table rows instead of group_id=3.
I wish I could write it like this, but it doesn't work at all:
select
user_id,
a/sum_a as percent_a,
b/sum_b as percent_b
from
users U,
(select
sum(a) as sum_a,
sum(b) as sum_b
from users where group_id=U.group_id) X
where group_id=3;
I don't want to make it as single query with "group by", because it has
a lot of fields and "group by" would be very long.
Regards,
Tomasz Myrta
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: jasiek@klaster.net
Subject: Re: sub-select with aggregate
In-Reply-To: <3DB674CD.50404@klaster.net>
* 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