Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Filters, Aggregates and TClientDataset

277 views
Skip to first unread message

David Clegg

unread,
Feb 25, 2002, 8:04:26 PM2/25/02
to
Am having a lot of fun using Filters and Aggregates with a
TClientDataset. If no filter is applied all is rosey, but as soon as I
apply a filter wierd things start to happen. Aggregate values before the
filter and with the filter applied appear to be correct, but if the
filter is removed, the aggregate values are inaccurate. I have attached
some sample code to show what I mean:

procedure TForm1.Button3Click(Sender: TObject);
var
i, BeforeFilter, WithFilter, AfterFilter:integer;
begin
//Clear the existing field defs and aggregates
with TClientDataset.Create(nil) do
try
//Add FieldDef
with FieldDefs.AddFieldDef do
begin
Name := 'Field1';
DataType := ftInteger;
end;

//Add an aggregate summing Field1
with Aggregates.Add do
begin
AggregateName := 'SumField1';
Expression := 'SUM(Field1)';
Active := true;
end;
AggregatesActive := true;

//Create and populate the Dataset
CreateDataset;
for i := 1 to 10 do
AppendRecord([i]);

//Aggregate before the filter applied (55)
BeforeFilter := Aggregates.Find('SumField1').Value;

//Aggregate with filter applied (10)
Filter := 'Field1 < 5';
Filtered := true;
WithFilter := Aggregates.Find('SumField1').Value;

//Aggregate after filter removed (should be 55, but returns 65).
Appears to
//be Aggregate with no filter + Aggregate with last filter
Filter := '';
Filtered := false;
AfterFilter := Aggregates.Find('SumField1').Value;

//Needed so we don't get 'Operation Not Applicable' error on close,
but
//thats another story :-)
AggregatesActive := false;
Aggregates.Clear;

Close;
ShowMessageFmt('Before:%d, Filtered:%d, After:%d',[BeforeFilter,
WithFilter, AfterFilter])
finally
Free;
end;
end;

Is this a known bug, or is it something to do with my code? Has anyone
else encountered this and know of a work around?
--
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com.

David Clegg

unread,
Feb 25, 2002, 9:19:26 PM2/25/02
to
Oops. Forgot to mention that I am using Delphi 5 Enterprise with Update
applied.
0 new messages