Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 09FF62E072B for ; Wed, 14 Nov 2007 07:57:01 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 52962-04 for ; Wed, 14 Nov 2007 07:56:47 -0400 (AST) Received: from pih-relay06.plus.net (pih-relay06.plus.net [212.159.14.133]) by postgresql.org (Postfix) with ESMTP id DC83D2E0DD0 for ; Wed, 14 Nov 2007 07:56:51 -0400 (AST) Received: from [84.51.143.99] (helo=server3.office.archonet.com) by pih-relay06.plus.net with esmtp (Exim) id 1IsGrP-0001mu-1B; Wed, 14 Nov 2007 11:56:49 +0000 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 41F9D274063; Wed, 14 Nov 2007 11:56:46 +0000 (GMT) Message-ID: <473AE27E.6050103@archonet.com> Date: Wed, 14 Nov 2007 11:56:46 +0000 From: Richard Huxton User-Agent: Thunderbird 2.0.0.6 (X11/20070728) MIME-Version: 1.0 To: Julien Cigar CC: pgsql-sql@postgresql.org Subject: Re: ALL() question References: <1195047542.3190.9.camel@frodon.be-bif.ulb.ac.be> In-Reply-To: <1195047542.3190.9.camel@frodon.be-bif.ulb.ac.be> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/62 X-Sequence-Number: 29590 Julien Cigar wrote: > > What I would like is a query that returns all the specimen_id of > this table which have _all_ the given test_bit_id. [snip] > With the following I got a syntax error: > select specimen_id > from specimen_test_bits > where test_bit_id = all(1,2,3,4); It's expecting an array here. You'd have to write = all('{1,2,3,4}') But that would have the same problem as... > The following works but no rows are returned : > select specimen_id > from specimen_test_bits > where test_bit_id = all(select id from test_bits where id in (1,2,3,4)); It's testing each row individually and of course one row can't match ALL four values. What you want to do is count the distinct values. Something like: SELECT specimen_id FROM foo GROUP BY specimen_id HAVING count(distinct test_bit_id) = 4 ; -- Richard Huxton Archonet Ltd