我有一张看起来像这样的表:
TYPE GROUP VALUE
----- ----- -----
0 0 10
0 0 60
0 1 20
1 0 30
1 1 40
1 1 10
我希望TYPE和TYPE的总数; GROUP。 在TYPE&上创建索引TYPE; GROUP
object ClientDataSet1: TClientDataSet
IndexDefs = <
item
Name = 'ClientDataSet1Index1'
Fields = 'TYPE'
GroupingLevel = 1
end
item
Name = 'ClientDataSet1Index2'
Fields = 'TYPE;GROUP'
GroupingLevel = 2
end>
IndexName = 'ClientDataSet1Index1'
并创建了两个聚合
object ClientDataSet1: TClientDataSet
Aggregates = <
item
Active = True
AggregateName = 'Agg1'
Expression = 'SUM(VALUE)'
GroupingLevel = 1
IndexName = 'ClientDataSet1Index1'
end
item
Active = True
AggregateName = 'Agg2'
Expression = 'SUM(VALUE)'
GroupingLevel = 2
IndexName = 'ClientDataSet1Index2'
end>
AggregatesActive = True
Agg2不会计算,因为ClientDataset索引设置为ClientDataSet1Index1。 如果ClientDataset.IndexName = ClientDataSet1Index2,则Agg2可以工作,但Agg1不会:( 它似乎不允许多个组合,因为我只能 一次指定一个clientdataset索引。
我错过了什么吗?
解决方法是克隆我的 clientdataset并创建另一个 汇总在那里。有点不方便。
[使用D2006]
由于
答案 0 :(得分:5)
它应该与第二个索引一起使用。以下程序似乎在D2007中有效(我没有D2006):
program cdsagg;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes, DB, DBClient;
procedure AppendRecord(DataSet: TClientDataSet; AType, AGroup, AValue: Integer);
begin
DataSet.Append;
try
DataSet.FieldByName('TYPE').AsInteger := AType;
DataSet.FieldByName('GROUP').AsInteger := AGroup;
DataSet.FieldByName('VALUE').AsInteger := AValue;
DataSet.Post;
except
DataSet.Cancel;
raise;
end;
end;
procedure Main;
var
DataSet: TClientDataSet;
begin
DataSet := TClientDataSet.Create(nil);
try
DataSet.FieldDefs.Add('TYPE', ftInteger);
DataSet.FieldDefs.Add('GROUP', ftInteger);
DataSet.FieldDefs.Add('VALUE', ftInteger);
DataSet.IndexDefs.Add('MyIndex', 'TYPE;GROUP', []);
DataSet.IndexName := 'MyIndex';
with DataSet.Aggregates.Add do
begin
AggregateName := 'AGG1';
Expression := 'SUM(VALUE)';
GroupingLevel := 0;
IndexName := 'MyIndex';
Active := True;
end;
with DataSet.Aggregates.Add do
begin
AggregateName := 'AGG2';
Expression := 'SUM(VALUE)';
GroupingLevel := 1;
IndexName := 'MyIndex';
Active := True;
end;
with DataSet.Aggregates.Add do
begin
AggregateName := 'AGG3';
Expression := 'SUM(VALUE)';
GroupingLevel := 2;
IndexName := 'MyIndex';
Active := True;
end;
DataSet.AggregatesActive := True;
DataSet.CreateDataSet;
DataSet.LogChanges := False;
AppendRecord(DataSet, 0, 0, 10);
AppendRecord(DataSet, 0, 0, 60);
AppendRecord(DataSet, 0, 1, 20);
AppendRecord(DataSet, 1, 0, 30);
AppendRecord(DataSet, 1, 1, 40);
AppendRecord(DataSet, 1, 1, 10);
DataSet.First;
while not DataSet.EOF do
begin
Writeln(Format('GROUP:'#9'%d'#9'TYPE:'#9'%d'#9'VALUE:'#9'%d'#9'AGG1:'#9'%d'#9'AGG2:'#9'%d'#9'AGG3:'#9'%d',
[
DataSet.FieldByName('GROUP').AsInteger,
DataSet.FieldByName('TYPE').AsInteger,
DataSet.FieldByName('VALUE').AsInteger,
Integer(DataSet.Aggregates[0].Value),
Integer(DataSet.Aggregates[1].Value),
Integer(DataSet.Aggregates[2].Value)
]
));
DataSet.Next;
end;
finally
DataSet.Free;
end;
end;
begin
try
Main;
except
on E: Exception do
begin
ExitCode := 1;
Writeln(Format('[%s] %s', [E.ClassName, E.Message]));
end;
end;
end.
产生的输出:
GROUP: 0 TYPE: 0 VALUE: 10 AGG1: 170 AGG2: 90 AGG3: 70
GROUP: 0 TYPE: 0 VALUE: 60 AGG1: 170 AGG2: 90 AGG3: 70
GROUP: 1 TYPE: 0 VALUE: 20 AGG1: 170 AGG2: 90 AGG3: 20
GROUP: 0 TYPE: 1 VALUE: 30 AGG1: 170 AGG2: 80 AGG3: 30
GROUP: 1 TYPE: 1 VALUE: 40 AGG1: 170 AGG2: 80 AGG3: 50
GROUP: 1 TYPE: 1 VALUE: 10 AGG1: 170 AGG2: 80 AGG3: 50