Re: Trouble with subqueries - Mailing list pgsql-sql

From Robert B. Easter
Subject Re: Trouble with subqueries
Date
Msg-id 0101192214380Q.02219@comptechnews
Whole thread Raw
In response to Re: Trouble with subqueries  (Jussi Vainionpää <jjvainio@cc.hut.fi>)
List 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@comptechnews.com ---------
-- CompTechNews Message Board http://www.comptechnews.com/ --
-- CompTechServ Tech Services http://www.comptechserv.com/ --
---------- http://www.comptechnews.com/~reaster/ ------------


pgsql-sql by date:

Previous
From: Jussi Vainionpää
Date:
Subject: Re: Trouble with subqueries
Next
From: Forest Wilkinson
Date:
Subject: unreferenced primary keys: garbage collection