Re: Which date/paper pairs are NOT represented?

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Andrew Perrin <andrew_perrin(at)unc(dot)edu>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Which date/paper pairs are NOT represented?
Date: 2002-01-07 18:10:02
Message-ID: Pine.LNX.4.30.0201071303480.805-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Andrew Perrin writes:

> papers: information about newspapers, including a unique paperid
> letters: information and text of letters to the editor, including the
> paperid
>
> There are, as you might expect, numerous letters on a given date in a
> given paper. What I need to know, though, is for what paper/date pairs in
> my sample I do NOT have any letters (since this probably represents a
> data-collection glitch). I know I can do:
>
> SELECT paper, date, count(date) FROM papers, letters WHERE
> papers.papercode = letters.papercode GROUP BY paper, date;
>
> but adding a HAVING count(date) < 1 seems to return 0 rows.

That really doesn't work, because grouping only groups values that are
actually there, so asking for groups with a count of 0 isn't going to give
an answer.

Moreover, in your specific application you're looking for papers without
any associated letters, so doing an inner join on papers and letters is
exactly the opposite of what you want to do.

Here's a possibility:

SELECT paper, date FROM papers WHERE papercode NOT IN (SELECT papercode
FROM letters);

--
Peter Eisentraut peter_e(at)gmx(dot)net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message steve boyle 2002-01-07 18:46:49 Re: Conditional test
Previous Message Michaell Oxtoby 2002-01-07 17:42:13 Uisng Procedures via JDBC