用TQuery对本地数据库有限制, local sql中有max, min, avg, sum...出现一定
要指定group by分句, 而且select的列表中不能有非计算字段不在group by 中,
例如:
select code, name, sum(salary) from "demo.db" group by code, name
合法但
select code, name, sum(salary) from "demo.db" group by code
^^^^^^
非法.
procedure TForm1.Table1CalcField(DataSet: TDataSet);
var
maxValue, minValue: integer;//or double according to acctural type
StartRec, i, recordNo: integer;
begin
RecordNo:= DataSet.RecNo;
if RecordNo < 10 then StartRec:=0
else StartNo:= RecordNo-10;
maxValue:=DataSet.FieldByName(AName).AsInteger;
minValue:=maxValue;
for i:=StartNo to RecordNo do
begin
if DataSet.FieldByName(AName).AsInteger > maxValue then
maxValue:=DataSet.FieldByName(AName).AsInteger;
if DataSet.FieldByName(AName).AsInteger < minValue then
minValue:=DataSet.FieldByName(AName).AsInteger;
end;
DataSet.FieldByName('MyCalcField').AsDouble:=maxValue/minValue;
end;
procedure TForm1.Table1CalcFields(DataSet: TDataSet);
var
Vcur: integer;
begin
Vcur := DataSet.FieldByName('EmpNo').AsInteger;
if (DataSet.RecNo Mod 11) = 0 then
begin
DataSet.FieldByName('f2').AsInteger := Vmax div Vmin;
Vmax := 0;
Vmin := high( integer );
end;
if Vcur > Vmax then Vmax := Vcur;
if Vcur < Vmin then Vmin := Vcur;
end;
var
Form1: TForm1;
Varray: array[0..Vn-1] of integer;
Vpointer: integer;
procedure TForm1.FormShow(Sender: TObject);
var
i: integer;
begin
for i:=0 to Vn-1 do Varray := 0;
end;
procedure TForm1.Table1CalcFields(DataSet: TDataSet);
var
i,j: integer;
begin
Varray[Vpointer] := DataSet.FieldByName('EmpNo').AsInteger;
j := Vpointer-1;
if j < 0 then j := Vn - 1;
inc(Vpointer);
Vpointer := Vpointer Mod Vn;
Vmax := 0;
Vmin := high( integer );
for i:=0 to Vn-1 do
begin
if Varray[j] > Vmax then Vmax := Varray[j];
if Varray[j] < Vmin then Vmin := Varray[j];
inc(j);
j := j mod Vn;
end;
DataSet.FieldByName('f2').AsInteger := Vmax div Vmin;
end;