Sort Collection on Aggregate field

Posts   
 
    
Zak
User
Posts: 10
Joined: 03-Jan-2006
# Posted on: 20-Apr-2006 00:37:18   

I am having trouble creating code that will sort a collection based on a aggregate field in a child collection.

Basically i want the sql to end up something like this.

select parent.field1, parent.field2 from parent inner join child on parent.field1=child.field1 group by parent.field1, parent.field2 order by Sum(child.field2) ASC

I can't seem to get the SUM part working. This is what I've come up with so far

sorter.Add(new SortClause((DALHelper.ChildFields.field2.SetAggregateFunction(AggregateFunction.Sum)),null, llbl.SortOperator.Ascending));

Your help and wisdom is anxiously awaited.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 20-Apr-2006 02:59:11   

Take a look at this post and let us know if it helps solve your problem.

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=2623&HighLight=1

If not post the code for what you have tried and we can help out a little more simple_smile .

Thanks, Brian

Zak
User
Posts: 10
Joined: 03-Jan-2006
# Posted on: 20-Apr-2006 17:22:38   

Nope that doesn't help, that is the thread i used to attempt this originally.

So far I've put this together but I need to add a groupby to the statement so that the sort works properly on the aggregate.

llbl.ISortExpression sorter = new llbl.SortExpression();
sorter.Add(new llbl.SortClause((DALHelper.ExpenseClaimItemFields.Amount
     .SetAggregateFunction(llbl.AggregateFunction.Sum)), null, llbl.SortOperator.Ascending));

llbl.IRelationCollection relations = new llbl.RelationCollection();        relations.Add(DALEntity.ExpenseClaimEntity.Relations.ExpenseClaimItemEntityUsingExpenseClaimId);

llbl.IPredicateExpression filter = new llbl.PredicateExpression();
ExpenseClaims = new DALCollection.ExpenseClaimCollection();
ExpenseClaims.GetMulti(filter, 0, sorter, relations);

The SQL that this outputs has an

ORDER BY Amount ASC

statement but no corresponding Group by statment.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 20-Apr-2006 18:04:50   

group by on entity fetches aren't supported, as you're fetching entity data, not grouped data.

Frans Bouma | Lead developer LLBLGen Pro
Zak
User
Posts: 10
Joined: 03-Jan-2006
# Posted on: 20-Apr-2006 18:42:11   

So i can do the following

                //set the group by clause and having statement
                llbl.IGroupByCollection groupBy = new llbl.GroupByCollection();
                groupBy.Add(DALHelper.ExpenseClaimItemTemplateFields.ExpenseClaimId);
                groupBy.HavingClause = new llbl.PredicateExpression((DALHelper.ExpenseClaimItemTemplateFields.Amount
                    .SetAggregateFunction(llbl.AggregateFunction.Sum)
                    >= txtTotalFilterAmount1.Text));
                
                //set the relations for the set query
                llbl.IRelationCollection relations = new llbl.RelationCollection();
                relations.Add(DALEntity.ExpenseClaimTemplateEntity.Relations.ExpenseClaimItemTemplateEntityUsingExpenseClaimId);

                //set the subselect filter.
                amtfilter.AddWithAnd(new llbl.FieldCompareSetPredicate(
                    DALHelper.ExpenseClaimTemplateFields.ExpenseClaimId,
                    DALHelper.ExpenseClaimItemTemplateFields.ExpenseClaimId,
                    llbl.SetOperator.In, null, relations, string.Empty, 0, null, false, groupBy));

to filter using a group by, but not with sorting?? That is unfortunate.

Zak
User
Posts: 10
Joined: 03-Jan-2006
# Posted on: 20-Apr-2006 22:12:59   

Is it possible to do this with a Typed List? I'm getting the same issue where the sql doesn't wrap the SUM around my sort by Amount.

What would be the recommended way to accomplish this using llblgen?

Zak
User
Posts: 10
Joined: 03-Jan-2006
# Posted on: 20-Apr-2006 23:21:43   

I've gotten the functionality i need by adding a view in the database and mapping an entity to that view.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 21-Apr-2006 09:02:34   

To recap: in v2 you can sort on an aggregate/expression. In v1 this isn't possible.

Frans Bouma | Lead developer LLBLGen Pro