Subqueries

Posts   
 
    
DerMohr
User
Posts: 3
Joined: 26-Feb-2006
# Posted on: 26-Feb-2006 11:35:52   

Hi,

I am still new to LLBLGen and I was not able to find the solution to my problem in the forum so far.

I want to do a query like:

SELECT Table1_ID, ... FROM Table1
INNER JOIN Table2 ON Table1.Table1_ID = Table2.Table1_ID
...
WHERE
  Table1.Table1_ID IN (a, list, of, IDs) 
  Table2.DateFrom = (
    SELECT MAX(Table2.DateFrom) 
    WHERE Table2.Table1_ID = Table1.Table1_ID)

Is this possible?

Regards, Jochen

sparmar2000 avatar
Posts: 341
Joined: 30-Nov-2003
# Posted on: 26-Feb-2006 12:11:59   

Jochen, Welcome to LLBLGen.

The example SQL should be possible. There are some good examples in the maul, but I suggest you have a quick look at "Best practises - How do I ... ?".

Please come back if this is not sufficient.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 26-Feb-2006 14:41:55   

Please check out the FieldCompareSetPredicate in the help, and Field Expressions and Aggregates.

Frans Bouma | Lead developer LLBLGen Pro
DerMohr
User
Posts: 3
Joined: 26-Feb-2006
# Posted on: 27-Feb-2006 14:07:05   

Well, I tried with FieldCompareSetPredicate, but no avail. I seem to be stuck.

I keep trying ...

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Feb-2006 14:22:07   

If the first where condition compares against a list of pre-defined IDs (an array of IDs) then use FieldCompareRangePredicate

If you are going to select the list of IDs in a sub-query then use FieldCompareSetPredicate

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Feb-2006 14:52:04   

Your Predicate Filter should look like the following:

SELECT Table1_ID, ... FROM Table1 INNER JOIN Table2 ON Table1.Table1_ID = Table2.Table1_ID ... WHERE Table1.Table1_ID IN (a, list, of, IDs) Table2.DateFrom = ( SELECT MAX(Table2.DateFrom) WHERE Table2.Table1_ID = Table1.Table1_ID)


// C#
IPredicateExpression filter = new PredicateExpression();

// First Predicate
int[] values = new int[3] {1, 2, 5};
filter.Add(new FieldCompareRangePredicate(
    Table1Fields.Table1_ID, null, values));
// which is equal to:
//filter.Add(Table1Fields.Table1_ID == values);

// Second Predicate
EntityField2 FiledMax = EntityFieldFactory.Create(Table2FieldIndex.DateFrom);
FiledMax.AggregateFunctionToApply = AggregateFunction.Max

filter.Add(new FieldCompareSetPredicate(EntityFieldFactory.Create(Table2FieldIndex.DateFrom), null, FiledMax , null, SetOperator.In, 
PredicateFactory.CompareExpression(Table2FieldIndex.Table1_ID, 
    ComparisonOperator.Equal,
    new Expression(Table1FieldIndex.Table1_ID))
, false));