gpt4 book ai didi

delphi - 客户端数据集、聚合和分组级别

转载 作者:行者123 更新时间:2023-12-02 01:12:43 25 4
gpt4 key购买 nike

我有一个如下所示的表格:

TYPE     GROUP     VALUE
----- ----- -----
0 0 10
0 0 60
0 1 20
1 0 30
1 1 40
1 1 10

我想要按类型和类型;组划分的总计。在 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 并创建另一个聚集在那里。有点不方便。

[使用 D2006]

谢谢

最佳答案

它应该与第二个索引一起使用。以下程序似乎可以在 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

关于delphi - 客户端数据集、聚合和分组级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com