Subject Re: Tough sql question
Author Svein Erling
--- In firebird-support@yahoogroups.com, Bob Murdoch wrote:
> I'm hoping someone can help with a tough question. I've been trying
> to come up with a single query to provide this answer, but have been
> unsuccessful:
>
> Given a table of orders and line items, I want to create a result
> set showing those orders than contain only line items matching a
> specific list. For example:
>
> Order 1:
> Apple
> Orange
>
> Order 2:
> Apple
> Orange
> Banana
>
> If the target list was [Apple,Orange], only Order 1 would meet the
> criteria (because order 2 contained something not in the list). If
> the target list was [Apple, Orange, Banana], both orders would
> match. If the target list was [Banana], either of the orders would
> match.
>
> I'm not opposed to creating a view and selecting from that.
>
> Off to dowse the flames coming from my ears,
>
> Bob M..

Hi Bob, I thought you'd relaxed for quite a few hours already, at
least it is a while since I saw this question in ib-general.

Reading your question as you wanting to return all orders which only
contain items in your list, the query could simply be:

SELECT <Something>
FROM Orders o
WHERE NOT EXISTS(
SELECT * FROM Orders o2
WHERE o2.id = o.id
AND o2.Product NOT IN ('Apple','Orange','Banana'))

By the way, I assumed you meant 'neither' rather than 'either' in this
sentence: "If the target list was [Banana], either of the orders would
match".

HTH,
Set