Re: count(*), EXISTS, indexes

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Itai Zukerman <zukerman(at)math-hat(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: count(*), EXISTS, indexes
Date: 2003-04-11 22:12:09
Message-ID: 20030411150926.C838-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, 11 Apr 2003, Itai Zukerman wrote:

> > CREATE TABLE A (x int PRIMARY KEY, real v);
> > CREATE TABLE B (x int);
> >
> > I'd like to calculate:
> >
> > SELECT sum(v) FROM A WHERE EXISTS (SELECT 1 FROM B WHERE A.x=B.x);
>
> This seems to be a reasonably-performing workaround:
>
> SELECT DISTINCT x INTO TEMP C FROM A,B WHERE A.x=B.x;
> SELECT sum(v) FROM A,C WHERE A.x=C.x;

Hmm, given that, would something like:

select sum(v) from
(select distinct on(x) x,v from a,b where a.x=b.x) as foo;

work?

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Dan Langille 2003-04-11 22:15:07 Re: SELECT INTO TEMP in Trigger?
Previous Message Tom Lane 2003-04-11 22:11:58 Re: count(*), EXISTS, indexes