pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Itai Zukerman <zukerman@math-hat.com>
To: Stephan Szabo <sszabo@megazone23.bigpanda.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: count(*), EXISTS, indexes
Date: Fri, 11 Apr 2003 18:18:59 -0400
Message-ID: <8765pkr830.fsf@matt.w80.math-hat.com> (raw)
In-Reply-To: <20030411150926.C838-100000@megazone23.bigpanda.com>
References: <20030411150926.C838-100000@megazone23.bigpanda.com>

>> >   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/;




view thread (14+ messages)  latest in thread

Message-ID: <8765pkr830.fsf@matt.w80.math-hat.com>
Permalink:  ../8765pkr830.fsf@matt.w80.math-hat.com/
Also on:    postgresql.org/message-id/8765pkr830.fsf@matt.w80.math-hat.com

 · 

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: zukerman@math-hat.com, sszabo@megazone23.bigpanda.com
  Subject: Re: count(*), EXISTS, indexes
  In-Reply-To: <8765pkr830.fsf@matt.w80.math-hat.com>

* 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