Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 5AA1E633873 for ; Fri, 9 Oct 2009 20:21:35 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 53437-02 for ; Fri, 9 Oct 2009 23:21:18 +0000 (UTC) X-Greylist: delayed 00:05:45.376137 by SQLgrey-1.7.6 Received: from mail03.insiderscore.com (mail03.insiderscore.com [72.5.31.50]) by mail.postgresql.org (Postfix) with ESMTP id 8E260632DBA for ; Fri, 9 Oct 2009 20:21:24 -0300 (ADT) Received: from [192.168.1.4] (vip.indieresearch.com [72.5.31.33]) (using SSLv3 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail03.insiderscore.com (Postfix) with ESMTP id BE8EF3780007; Fri, 9 Oct 2009 19:15:36 -0400 (EDT) X-DomainKeys: Sendmail DomainKeys Filter v1.0.1 mail03.insiderscore.com BE8EF3780007 DomainKey-Signature: a=rsa-sha1; s=is; d=insiderscore.com; c=simple; q=dns; b=VYtpmuAiTs3qhCkkATP8ugeC+kL1NDeCqiuKftS/yofrmA11if+lBsAvqxZ+sbsfe FA5nii8qH7eB5HkhkMHg3tpaYd/jjM/tlZTy+rmCTiNqURsX85FqDwJErdzDcVB Subject: Re: Existential quantifier From: Richard Albright To: Dag-Erling =?ISO-8859-1?Q?Sm=F8rgrav?= Cc: pgsql-sql@postgresql.org In-Reply-To: <86bpkgqiv8.fsf@ds4.des.no> References: <86bpkgqiv8.fsf@ds4.des.no> Content-Type: text/plain; charset="UTF-8" Date: Fri, 09 Oct 2009 19:11:09 -0400 Message-Id: <1255129869.11289.4.camel@indie-desktop> Mime-Version: 1.0 X-Mailer: Evolution 2.26.1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.74 tagged_above=-10 required=5 tests=BAYES_20=-0.74 X-Spam-Level: X-Archive-Number: 200910/36 X-Sequence-Number: 33235 you could use distinct on select distinct on (fs.film.title, fs.film.year ) title, year from fs.film left join fs.star on fs.film.id = fs.star.film where fs.star.last = 'Sheen'; On Sat, 2009-10-10 at 00:53 +0200, Dag-Erling Smørgrav wrote: > Consider the attached schema (filmstars.sql), which is a poorly designed > database of films and actors. The following query gives me a list of > films in which either Charlie or Martin Sheen starred: > > select fs.film.title, fs.film.year > from fs.film left join fs.star on fs.film.id = fs.star.film > where fs.star.last = 'Sheen' > group by fs.film.title, fs.film.year; > > Is there a way to do this without the "group by" clause? > > DES > plain text document attachment (filmstars.sql) > drop schema fs cascade; > > create schema fs; > > create table fs.film ( > id serial not null primary key, > title varchar not null unique, > year integer not null > ); > > create table fs.star ( > id serial not null primary key, > film integer not null references fs.film(id), > last varchar not null, > first varchar not null, > unique (film, last, first) > ); > > insert into fs.film(title, year) values ('Apocalypse Now', 1979); > insert into fs.star(film, last, first) > select id, 'Sheen', 'Martin' from fs.film where title = 'Apocalypse Now'; > insert into fs.star(film, last, first) > select id, 'Brando', 'Marlon' from fs.film where title = 'Apocalypse Now'; > insert into fs.star(film, last, first) > select id, 'Duvall', 'Robert' from fs.film where title = 'Apocalypse Now'; > insert into fs.star(film, last, first) > select id, 'Ford', 'Harrison' from fs.film where title = 'Apocalypse Now'; > > insert into fs.film(title, year) values ('Blade Runner', 1982); > insert into fs.star(film, last, first) > select id, 'Ford', 'Harrison' from fs.film where title = 'Blade Runner'; > insert into fs.star(film, last, first) > select id, 'Young', 'Sean' from fs.film where title = 'Blade Runner'; > insert into fs.star(film, last, first) > select id, 'Hauer', 'Rutger' from fs.film where title = 'Blade Runner'; > insert into fs.star(film, last, first) > select id, 'Hannah', 'Daryl' from fs.film where title = 'Blade Runner'; > > insert into fs.film(title, year) values ('Platoon', 1986); > insert into fs.star(film, last, first) > select id, 'Sheen', 'Charlie' from fs.film where title = 'Platoon'; > insert into fs.star(film, last, first) > select id, 'Dafoe', 'Willem' from fs.film where title = 'Platoon'; > insert into fs.star(film, last, first) > select id, 'Berenger', 'Tom' from fs.film where title = 'Platoon'; > > insert into fs.film(title, year) values ('Wall Street', 1987); > insert into fs.star(film, last, first) > select id, 'Douglas', 'Michael' from fs.film where title = 'Wall Street'; > insert into fs.star(film, last, first) > select id, 'Sheen', 'Charlie' from fs.film where title = 'Wall Street'; > insert into fs.star(film, last, first) > select id, 'Hannah', 'Daryl' from fs.film where title = 'Wall Street'; > insert into fs.star(film, last, first) > select id, 'Sheen', 'Martin' from fs.film where title = 'Wall Street'; -- Rick Albright Senior Quantitative Analyst Insiderscore LLC ralbright@insiderscore.com