pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Albright <ralbright@insiderscore.com>
To: Dag-Erling Smørgrav <des@des.no>
Cc: pgsql-sql@postgresql.org
Subject: Re: Existential quantifier
Date: Fri, 09 Oct 2009 19:11:09 -0400
Message-ID: <1255129869.11289.4.camel@indie-desktop> (raw)
In-Reply-To: <86bpkgqiv8.fsf@ds4.des.no>
References: <86bpkgqiv8.fsf@ds4.des.no>

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




view thread (6+ messages)  latest in thread

Message-ID: <1255129869.11289.4.camel@indie-desktop>
Permalink:  ../1255129869.11289.4.camel@indie-desktop/
Also on:    postgresql.org/message-id/1255129869.11289.4.camel@indie-desktop

 · 

reply

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: ralbright@insiderscore.com, des@des.no
  Subject: Re: Existential quantifier
  In-Reply-To: <1255129869.11289.4.camel@indie-desktop>

* 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