Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 944352E174F for ; Wed, 14 Nov 2007 09:20:24 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 32852-07 for ; Wed, 14 Nov 2007 09:20:05 -0400 (AST) Received: from ptb-relay02.plus.net (ptb-relay02.plus.net [212.159.14.213]) by postgresql.org (Postfix) with ESMTP id D7DB62E0F8F for ; Wed, 14 Nov 2007 09:20:16 -0400 (AST) Received: from [84.51.143.99] (helo=server3.office.archonet.com) by ptb-relay02.plus.net with esmtp (Exim) id 1IsIAA-0000vO-R5; Wed, 14 Nov 2007 13:20:14 +0000 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 826E8274063; Wed, 14 Nov 2007 13:20:14 +0000 (GMT) Message-ID: <473AF60E.8090403@archonet.com> Date: Wed, 14 Nov 2007 13:20:14 +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> <473AE27E.6050103@archonet.com> <1195051800.3190.18.camel@frodon.be-bif.ulb.ac.be> In-Reply-To: <1195051800.3190.18.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/67 X-Sequence-Number: 29595 Julien Cigar wrote: > On Wed, 2007-11-14 at 11:56 +0000, Richard Huxton wrote: >> 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 >> ; >> > > I don't think it would work, for example if I have: > specimen_id | test_bit_id > ------------+------------ > 100 1 > 100 3 > 101 1 > 101 2 > > the test_bit_ids are parameters, so with the given test_bit_id 1,3 it > would return specimen_id 101 too, which I don't want ... Not if you test for what you want too: ... FROM foo WHERE test_bit_id = ANY ('{1,3}') ...or... WHERE test_bit_id IN (1,3) > What I would like is the specimen_id which match _exactly_ the given > test_bit_ids, so it should return only 100 in this example .. > > from the documentation ALL() can take a subquery too, not only an ARRAY > (http://www.postgresql.org/docs/8.2/static/functions-subquery.html) Yes, but that doesn't help with your query - one row can't match ALL your values. -- Richard Huxton Archonet Ltd