Re: count(*), EXISTS, indexes

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

>> > CREATE TABLE A (x int PRIMARY KEY, real v);
>> > CREATE TABLE B (x int);
>> >
>> > 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;

Excellent. Thanks!

--
Itai Zukerman <http://www.math-hat.com/~zukerman/>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2003-04-11 22:36:23 Re: count(*), EXISTS, indexes
Previous Message Itai Zukerman 2003-04-11 22:15:09 Re: SELECT INTO TEMP in Trigger?