Building a Group By Clause

Posts   
 
    
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 29-Nov-2004 11:12:21   

Based on your samples in the documentation, you build your fields and group by clauses as such:


ResultsetFields fields = new ResultsetFields(3);
fields.DefineField(EmployeeFieldIndex.FirstName, 0, "FirstNameManager", "Manager");
fields.DefineField(EmployeeFieldIndex.LastName, 1, "LastNameManager", "Manager");
fields.DefineField(EmployeeFieldIndex.LastName, 2, "AmountEmployees", "Employee", AggregateFunction.Count);
// .... extra code omitted
IGroupByCollection groupByClause = new GroupByCollection();
groupByClause.Add(fields[0]);
groupByClause.Add(fields[1]);

To make my code a bit easier to read, I have build my code field and group by code as such:


// create the group by clause & result set fields
ResultsetFields fields = CustomResultsetFields.AuthorContentList();
IGroupByCollection groupByClause = new GroupByCollection();
for (int i = 0; i < fields.Count ; i++)
{
    IEntityField2 field = fields[i] as IEntityField2;
    if (field.AggregateFunctionToApply == AggregateFunction.None)
    {
        groupByClause.Add(field);
    }
}

Based on my knowledge of SQL Server, you must include any fields not being aggregated in the group by clause.

So the question is this: Will using the field.AggregateFunctionToApply == AggregateFunction.None statement in the if block above always guarantee that all non-aggregated fileds in a dynamic typed list are included in the group by clause? I think it does. Also, do I need to account for expression fields at all?

Ultimately I would like to move the "for i" clause into a seperate function so I can write code like this:


IGroupByClause groupBy = MyGroupByClause(fields);

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 29-Nov-2004 11:40:34   

Devildog74 wrote:

To make my code a bit easier to read, I have build my code field and group by code as such:


// create the group by clause & result set fields
ResultsetFields fields = CustomResultsetFields.AuthorContentList();
IGroupByCollection groupByClause = new GroupByCollection();
for (int i = 0; i < fields.Count ; i++)
{
    IEntityField2 field = fields[i] as IEntityField2;
    if (field.AggregateFunctionToApply == AggregateFunction.None)
    {
        groupByClause.Add(field);
    }
}

Based on my knowledge of SQL Server, you must include any fields not being aggregated in the group by clause.

So the question is this: Will using the field.AggregateFunctionToApply == AggregateFunction.None statement in the if block above always guarantee that all non-aggregated fileds in a dynamic typed list are included in the group by clause? I think it does.

It does simple_smile . By default all fields have AggregateFunction.None applied. So unless you explicitly set an AggregateFunction, they don't have an aggregate function applied.

Also, do I need to account for expression fields at all?

No, as the RDBMS sql parser will only care for aggregated columns and non-aggregated columns. This means that fields with an expression and no aggregate function are not aggregated and that column thus has to be used in the group by.

Frans Bouma | Lead developer LLBLGen Pro