Re: SQL question: checking all required items

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: SQL question: checking all required items
Date: 2007-08-11 18:09:13
Message-ID: 46BDFB49.2020108@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/08/2007 21:42, Scott Marlowe wrote:

> Show us the query when you're done, I'm sure there are enough folks
> who'd like to see your solution.

Here's what I came up with:

select distinct ip.person_id from items_for_people ip
where exists (
(
select item_id from items
where is_required = true
)
except
(
select ip2.item_id from items_for_people ip2
inner join items i on (ip2.item_id = i.item_id)
where ip2.person_id = ip.person_id
and i.is_required = true
)
)

This finds all those who don't have all the required items, whatever
else they may have.

Comments and improvements are welcome!

Thanks for the help,

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod(at)iol(dot)ie
---------------------------------------------------------------

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Javier Fonseca V. 2007-08-11 18:45:47 Re: Trigger Procedure Error: NEW used in query that is not in a rule
Previous Message Hannes Dorbath 2007-08-11 18:05:49 Re: virtual database