No.1 循环计算
var
  S: Real;
begin
  Table1.First;
  S := 0;
  while not Table1.Eof do begin
    if Table1.FieldByName('字段2').AsString = 'A'  then
      S := S + Table1.FieldByName('字段1').AsFloat;
    Table1.Next;
  end;
  Label1.Caption := FloatToStr(S);
end;No.2 SQL查询 //建议
Query1.SQL.Text := 'SELECT SUM(字段1) FROM 表名 WHERE 字段2="A"';
Query1.Open;
Label1.Caption := Query1.Fields[0].AsString;
Query1.Close;

解决方案 »

  1.   

    No.1 循环计算
    var
      S: Real;
    begin
      Table1.First;
      S := 0;
      while not Table1.Eof do begin
        if Table1.FieldByName('字段2').AsString = 'A'  then
          S := S + Table1.FieldByName('字段1').AsFloat;
        Table1.Next;
      end;
      Label1.Caption := FloatToStr(S);
    end;No.2 SQL查询 //建议
    Query1.SQL.Text := 'SELECT SUM(字段1) FROM 表名 WHERE 字段2="A"';
    Query1.Open;
    Label1.Caption := Query1.Fields[0].AsString;
    Query1.Close;
      

  2.   

    No.1 循环计算
    var
      S: Real;
    begin
      Table1.First;
      S := 0;
      while not Table1.Eof do begin
        if Table1.FieldByName('字段2').AsString = 'A'  then
          S := S + Table1.FieldByName('字段1').AsFloat;
        Table1.Next;
      end;
      Label1.Caption := FloatToStr(S);
    end;No.2 SQL查询 //建议
    Query1.SQL.Text := 'SELECT SUM(字段1) FROM 表名 WHERE 字段2="A"';
    Query1.Open;
    Label1.Caption := Query1.Fields[0].AsString;
    Query1.Close;
      

  3.   


    Query1.Clear;
    Query1.SQL.clearQuery1.SQL.Add(Sum(字段1)as FTotal From Table 
    Where 字段2='A');Query1.Open;Label1.Caption:=FormatFloat(Query.FieldByName('FTotal').asFloat);Query1.Close;
    ---------------------
      

  4.   

    改一句Label1.Caption:=FormatFloat('0.00',Query.FieldByName('FTotal').asFloat);
    ---------------------
      

  5.   

    Query1.SQL.Add(select Sum(字段1)as FTotal From Table 
    Where 字段2='A');
      

  6.   

    with table1 do
    begin
    close;
    sql.text:='select a.* from(select 1,sum(2) as n from table group by 1) as a where a.2=''A''';
    open;
    label1.caption:=fieldbyname('n').asinteger;
    end;
      

  7.   

    Query1.Clear;
    Query1.SQL.clearQuery1.SQL.Add(select Sum(字段1)as Total From Table 
    Where 字段2='A');Query1.Open;Label1.Caption:=Query.FieldByName('Total').asstring;