//先在窗體上放置 ClIEntDataSet1、Button1 然後:
{ 建表 }
procedure TForm1.FormCreate(Sender: TObject);
begin
with ClIEntDataSet1 do begin
{ 定義表 }
FIEldDefs.Add('班級', ftWideString, 4);
FIEldDefs.Add('姓名', ftWideString, 8);
FIEldDefs.Add('年齡', ftByte);
FIEldDefs.Add('語文成績', ftFloat);
FIEldDefs.Add('數學成績', ftFloat);
{ 建立表 }
CreateDataSet;
{ 添加 18 條測試數據 }
AppendRecord(['一班', '趙錢', 8, 81, 98]);
AppendRecord(['一班', '孫李', 9]); { 假如他沒有成績 }
AppendRecord(['一班', '周吳', 8, 82, 97]);
AppendRecord(['三班', '鄭王', 9, 83, 96]);
AppendRecord(['三班', '馮陳', 8, 84, 95]);
AppendRecord(['三班', '諸衛', 9, 85, 94]);
AppendRecord(['二班', '蔣沈', 8, 86, 93]);
AppendRecord(['二班', '韓楊', 9, 87, 92]);
AppendRecord(['二班', '朱秦', 8, 88, 91]);
AppendRecord(['一班', '尤許', 9, 89, 90]);
AppendRecord(['一班', '何呂', 8, 99, 89]);
AppendRecord(['一班', '施張', 9, 98, 88]);
AppendRecord(['三班', '孔曹', 8, 97, 87]);
AppendRecord(['三班', '嚴華', 9, 96, 86]);
AppendRecord(['三班', '金魏', 8, 95, 85]);
AppendRecord(['二班', '陶姜', 9, 94, 84]);
AppendRecord(['二班', '戚謝', 8, 93, 83]);
AppendRecord(['二班', '鄒喻', 9, 92, 82]);
end;
{ 備份這些數據給後面測試用 }
ClIEntDataSet1.MergeChangeLog;
ClIEntDataSet1.SaveToFile('C:\Temp\Test.xml', dfXMLUTF8);
end;
{ 統計 }
procedure TForm1.Button1Click(Sender: TObject);
begin
{ (有效的)人員總數 }
with ClIEntDataSet1.Aggregates.Add do begin
Expression := 'Count(語文成績)';
Active := True;
end;
{ 語文總成績 }
with ClIEntDataSet1.Aggregates.Add do begin
Expression := 'Sum(語文成績)';
Active := True;
end;
{ 語文平均成績 }
with ClIEntDataSet1.Aggregates.Add do begin
Expression := 'Avg(語文成績)';
Active := True;
end;
{ 語文最高成績 }
with ClIEntDataSet1.Aggregates.Add do begin
Expression := 'Max(語文成績)';
Active := True;
end;
{ 語文最低成績 }
with ClIEntDataSet1.Aggregates.Add do begin
Expression := 'Min(語文成績)';
Active := True;
end;
{ 語文和數學的總成績 }
with ClIEntDataSet1.Aggregates.Add do begin
Expression := 'Sum(語文成績) + Sum(數學成績)';
Active := True;
end;
{ 執行統計 }
ClIEntDataSet1.AggregatesActive := True;
{ 查看統計結果 }
with ClIEntDataSet1 do begin
ShowMessageFmt('%d', [Integer(Aggregates[0].Value)]); { 17 }
ShowMessageFmt('%.2f', [Double(Aggregates[1].Value)]); { 1529.00 }
ShowMessageFmt('%.2f', [Double(Aggregates[2].Value)]); { 89.94 }
ShowMessageFmt('%.2f', [Double(Aggregates[3].Value)]); { 99.00 }
ShowMessageFmt('%.2f', [Double(Aggregates[4].Value)]); { 81.00 }
ShowMessageFmt('%.2f', [Double(Aggregates[5].Value)]); { 3059.00 }
end;
end;
設計時使用統計的過程(使用了上面的測試值):
查看原圖(大圖)