Subject Re: [firebird-support] Re: using ALL or ANY
Author Svein Erling Tysvaer
To further build on your example:

Customer 1
season 1 2000
season 2 1500
season 3 3000

Customer 2
season 1 1500
season 2 2000
season 3 500

Customer 3
season 1 1200
season 3 1700

Customer 4
season 3 2000

The second/third not exists eliminates customer 3 and customer 4 from
the result set. I assume you didn't want to return customer 3, since
customer 4 is a new customer, I'm more uncertain whether you want him in
the result set or not.

HTH,
Set
-tried to answer 12 hours ago, but there were problems preventing the
mail from getting through

Rick Roen wrote:
> Thanks Sven,
>
> The nested "exists" DO work in 1.5.3.
>
> I'm have not yet verified the results but I get a result set.
>
> I can't follow the purpose of the second "not exists..."?
>
> Rick
>
> --- In firebird-support@yahoogroups.com, Svein Erling Tysvaer wrote:
>
>> Hmm, this sounds unusual and requires some thinking.
>>
>> SELECT C.*
>> from CUSTOMERS C
>> where not exists(Select O.Season, SUM(O.Total) From ORDERS O
>> where O.CUSTNUM = C.IDNUM Group by O.Season HAVING SUM(O.Total) <
> 1000)
>> and not exists(select * from ORDERS O2
>> where not exists(select * from ORDERS O3 where O3.CustNUM = C.IDNUM
>> and O3.Season = O2.Season))
>>
>> Now, I've never tried using GROUP BY within a subselect and don't know
>> whether that works in Firebird 1.5 (I doubt it). The second not exists
>> (the one that is nested) should work and ensures that customers with no
>> orders in a season are eliminated from the result.