From: Robert B. Easter <reaster@comptechnews.com>
To: Jussi Vainionpää <jjvainio@cc.hut.fi>
To: pgsql-sql@postgresql.org
Subject: Re: Trouble with subqueries
Date: Fri, 19 Jan 2001 22:14:38 -0500
Message-ID: <0101192214380Q.02219@comptechnews> (raw)
In-Reply-To: <3A68CB54.224A7BE7@cc.hut.fi>
References: <11131831464.20010119180542@vpcit.ru>
<3A68CB54.224A7BE7@cc.hut.fi>
Stuff like this is possible in 7.1:
SELECT m.name, m.length, count(r.rating) AS fives
FROM movies m LEFT JOIN (SELECT * FROM ratings WHERE rating = 5) AS r
ON m.name = r.name
GROUP BY m.name, m.length;
I think that would work. You'd want to try different queries with EXPLAIN to
see what looks best.
> But I did find a solution:
> SELECT movies.name, movies.length,
> COUNT(CASE WHEN ratings.name=movies.name
> AND rating='5' THEN true END) AS fives
> FROM ratings, movies GROUP BY movies.name, movies.length;
>
> But I don't quite understand why I need to have movies.length in the GROUP
> BY -clause?
When doing a GROUP BY, you can only select grouped columns. You cannot
select other columns (except in group aggregates) since there can be more
than one possible value for them if the group has more than one row. The
database can't know which row in the group from which to get the length
field. If length is grouped, there is only one possible value for it in the
whole group, so I knows what value to get (the only one). Group aggregates
are allowed on the ungrouped columns (and the grouped columns too) since it
is not ambiguous - not single value to trying to be selected. When you do a
GROUP BY, your table is partitioned into blocks of rows where the GROUPed BY
columns are the same for all rows in the group. Only one row can result from
each group of a grouped table. Aggregate functions used in returning a group
row from a grouped table are aggregates on the group returned by that row,
not the whole (ungrouped) table. Hope that makes sense.
--
-------- Robert B. Easter reaster@comptechnews.com ---------
-- CompTechNews Message Board http://www.comptechnews.com/ --
-- CompTechServ Tech Services http://www.comptechserv.com/ --
---------- http://www.comptechnews.com/~reaster/ ------------
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: reaster@comptechnews.com, jjvainio@cc.hut.fi
Subject: Re: Trouble with subqueries
In-Reply-To: <0101192214380Q.02219@comptechnews>
References: <11131831464.20010119180542@vpcit.ru> <3A68CB54.224A7BE7@cc.hut.fi> <0101192214380Q.02219@comptechnews>
* 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