pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Julien Cigar <jcigar@ulb.ac.be>
Cc: pgsql-sql@postgresql.org
Subject: Re: ALL() question
Date: Wed, 14 Nov 2007 11:56:46 +0000
Message-ID: <473AE27E.6050103@archonet.com> (raw)
In-Reply-To: <1195047542.3190.9.camel@frodon.be-bif.ulb.ac.be>
References: <1195047542.3190.9.camel@frodon.be-bif.ulb.ac.be>

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



view thread (10+ messages)  latest in thread

Message-ID: <473AE27E.6050103@archonet.com>
Permalink:  ../473AE27E.6050103@archonet.com/
Also on:    postgresql.org/message-id/473AE27E.6050103@archonet.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: dev@archonet.com, jcigar@ulb.ac.be
  Subject: Re: ALL() question
  In-Reply-To: <473AE27E.6050103@archonet.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