Re: Trouble with subqueries

From: "Robert B(dot) Easter" <reaster(at)comptechnews(dot)com>
To: Jussi Vainionpää <jjvainio(at)cc(dot)hut(dot)fi>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Trouble with subqueries
Date: 2001-01-20 03:14:38
Message-ID: 0101192214380Q.02219@comptechnews
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)comptechnews(dot)com ---------
-- CompTechNews Message Board http://www.comptechnews.com/ --
-- CompTechServ Tech Services http://www.comptechserv.com/ --
---------- http://www.comptechnews.com/~reaster/ ------------

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ian Harding 2001-01-20 04:31:09 Re: pl/pgsql Limits
Previous Message Jussi Vainionpää 2001-01-19 23:18:44 Re: Trouble with subqueries