Double join condition

Posts   
 
    
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 18-Feb-2010 16:01:22   

Hi,

I need to filter a entity on 2 different conditions on a related entity, and both have to qualify:

For example: I require to get an Order which contains apples, but not bananas.

Therefore I would like to join the OrderItem table and filter: OrderItem.ProductName = 'apples' AND OrderItem.ProductName != 'bananas'

Is there a way to perform this using regular PredicateExpressions, or do I require to use "FieldCompareSetPredicates"?

Please advice,

Using: Adapter, 2.6

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 18-Feb-2010 21:19:04   

Something like...


RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.Relations.Add(OrderEntity.Relations.OrderItemsUsingOrderId);
bucket.PredicateExpression.Add(OrderItemFields.ProductName=="apples");
bucket.PredicateExpression.AddWithAnd(OrderItemFields.ProductName!="bananas");

Matt

arschr
User
Posts: 893
Joined: 14-Dec-2003
# Posted on: 19-Feb-2010 13:51:47   

RelationPredicateBucket bucket = new RelationPredicateBucket(); bucket.Relations.Add(OrderEntity.Relations.OrderItemsUsingOrderId); bucket.PredicateExpression.Add(OrderItemFields.ProductName=="apples"); bucket.PredicateExpression.AddWithAnd(OrderItemFields.ProductName!="bananas");

I don't think so, because if an individual row has a product name of apples it will always be not equal to bananas!

He needs the relation added twice, maybe one with an alias, the first filtered on = apples and the second independent on filtered on != bananas

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Feb-2010 18:47:26   

gabrielk wrote:

Hi,

I need to filter a entity on 2 different conditions on a related entity, and both have to qualify:

For example: I require to get an Order which contains apples, but not bananas.

Therefore I would like to join the OrderItem table and filter: OrderItem.ProductName = 'apples' AND OrderItem.ProductName != 'bananas'

It's not clear if by "I would like to join the OrderItem" you meant a Join to filter the Orders, or you want to prefetch the OrderItems. Could you please show us an approximate SQL of such query?

David Elizondo | LLBLGen Support Team
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 22-Feb-2010 19:49:41   

A query could be:


SELECT * FROM Orders WHERE 
(
    /* REQUIRE At least 1 apples */ 
    SELECT COUNT(*)  FROM OrderItem 
    WHERE 
    OrderItem.OrderId=Order.OrderId AND 
    ProductName='apples'
) > 0 
AND
(
    /* Don't allow banans */ 
    SELECT COUNT(*)  FROM OrderItem 
    WHERE 
    OrderItem.OrderId=Order.OrderId AND 
    ProductName='bananas'
) = 0 

Is this possible using a single query/fetch in LLBGen?

Cheers, Gab

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 23-Feb-2010 08:57:16   

I think the following query is easier:

SELECT * FROM Order
WHERE 
OrderId IN (SELECT OrderId From OrderItem WHERE ProductName = 'apple')
AND 
OrderId NOT IN (SELECT OrderId From OrderItem WHERE ProductName = 'banana')

And this can be easily implemented by 2 FieldCompareSetPredicate filters.