Thanks for you help on both this and my other post - I know they are related...
I have now amended to the code to
public static DataTable MGRGetProductValueTable(DateTime startDate, DateTime endDate, bool isSales, int dealStateID, bool useWeightedValues)
{
try
{
IDataAccessAdapter myAdapter;
myAdapter = LLBLAdapter.GetNew();
ResultsetFields myFields = new ResultsetFields(5);
myFields.DefineField(CubeDealItemUserFieldIndex.Deleted, 0, "Deleted","CUBE_DEAL_ITEM_USER");
myFields.DefineField(CubeDealItemUserFieldIndex.DealStateID, 1, "DealStateID","CUBE_DEAL_ITEM_USER");
myFields.DefineField(CubeDealItemUserFieldIndex.ProductID, 2, "ProductID","PRODUCT");
myFields.DefineField(ProductFieldIndex.Name, 3, "ProductName","PRODUCT");
if (useWeightedValues)
{
if (isSales)
{
myFields.DefineField(CubeDealItemUserFieldIndex.UserWeightedLineSaleValue, 4, "Value","CUBE_DEAL_ITEM_USER",AggregateFunction.Sum);
}
else
{
myFields.DefineField(CubeDealItemUserFieldIndex.UserWeightedLineProfitValue, 4, "Value","CUBE_DEAL_ITEM_USER",AggregateFunction.Sum);
}
}
else
{
if (isSales)
{
myFields.DefineField(CubeDealItemUserFieldIndex.UserLineSaleValue, 4, "Value","CUBE_DEAL_ITEM_USER",AggregateFunction.Sum);
}
else
{
myFields.DefineField(CubeDealItemUserFieldIndex.UserLineProfitValue, 4, "Value","CUBE_DEAL_ITEM_USER",AggregateFunction.Sum);
}
}
IRelationPredicateBucket myBucket = new RelationPredicateBucket();
myBucket.Relations.Add(CubeDealItemUserEntity.Relations.ProductEntityUsingProductID);
IPredicateExpression myExpression = new PredicateExpression();
myExpression.Add(PredicateFactory.CompareValue(CubeDealItemUserFieldIndex.CubeDate, ComparisonOperator.LessEqual, endDate));
myExpression.AddWithAnd(PredicateFactory.CompareValue(CubeDealItemUserFieldIndex.CubeDate, ComparisonOperator.GreaterEqual, startDate));
myBucket.PredicateExpression.Add(myExpression);
IGroupByCollection myGroupByClause = new GroupByCollection();
myGroupByClause.Add(myFields[0]);
myGroupByClause.Add(myFields[1]);
myGroupByClause.Add(myFields[2]);
myGroupByClause.Add(myFields[3]);
IPredicateExpression myHavingFilter = new PredicateExpression();
myHavingFilter.Add(new FieldCompareValuePredicate(myFields[1],null,ComparisonOperator.Equal,dealStateID));
myHavingFilter.AddWithAnd(new FieldCompareValuePredicate(myFields[0],null,ComparisonOperator.Equal,0));
myGroupByClause.HavingClause = myHavingFilter;
ISortExpression mySorter = new SortExpression(new SortClause(myFields[4],null,SortOperator.Descending));
DataTable myDynamicList = new DataTable();
myAdapter.FetchTypedList(myFields, myDynamicList, myBucket, 0, mySorter, true, myGroupByClause);
return myDynamicList;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return null;
}
}
Whiuch generates the SQL:
Generated Sql query:
Query: SELECT CUBE_DEAL_ITEM_USER.DELETED AS Deleted,CUBE_DEAL_ITEM_USER.DEAL_STATE_ID AS DealStateID,PRODUCT.PRODUCT_ID AS ProductID,PRODUCT.NAME AS ProductName,SUM(CUBE_DEAL_ITEM_USER.USER_WEIGHTED_LINE_SALE_VALUE) AS Value FROM ( PRODUCT INNER JOIN CUBE_DEAL_ITEM_USER ON PRODUCT.PRODUCT_ID=CUBE_DEAL_ITEM_USER.PRODUCT_ID) WHERE ( ( CUBE_DEAL_ITEM_USER.CUBE_DATE <= @CubeDate1 And CUBE_DEAL_ITEM_USER.CUBE_DATE >= @CubeDate2)) GROUP BY CUBE_DEAL_ITEM_USER.DELETED,CUBE_DEAL_ITEM_USER.DEAL_STATE_ID,PRODUCT.PRODUCT_ID,PRODUCT.NAME HAVING ( CUBE_DEAL_ITEM_USER.DEAL_STATE_ID = @DealStateID3 And CUBE_DEAL_ITEM_USER.DELETED = @Deleted4) ORDER BY Value DESC
Parameter: @CubeDate1 : Date. Length: 8. Precision: 0. Scale: 0. Direction: Input. Value: 31/07/2005 10:12:08.
Parameter: @CubeDate2 : Date. Length: 8. Precision: 0. Scale: 0. Direction: Input. Value: 01/08/2004 10:12:08.
Parameter: @DealStateID3 : Int32. Length: 4. Precision: 0. Scale: 0. Direction: Input. Value: 200.
Parameter: @Deleted4 : Int32. Length: 4. Precision: 0. Scale: 0. Direction: Input. Value: 0.
Which looks spot on to me...
However I get the Firebird Error as per my other post:
"Dynamic SQL Error.... Token unknown... Line 1 Char 211 Value"
I.E. I dont thisnk it likes the fact that I have called the Order by column "Value" - it must be a reserved word... so I change the column alias to "myVal" and I get a different error
"Dynamic SQL Error.... Column unknown... myVal"
Now I am confused... I hope this is all clear to you!!