Board index » delphi » Problems with SUM in SQL statement

Problems with SUM in SQL statement

I am working with the dBase table "Customers.dbf".
I have to sum the values of the field SALARY and
store the result in a variable called "tot".
I wrote this code:

procedure SumRecords;
var tot:double;
begin
  tot:=0;
  Query1.Close;
  Query1.SQL.Clear;
  Query1.SQL.Add('SELECT NAME,ADDRESS,CITY,SALARY,');
  Query1.SQL.Add('SUM (SALARY) as tot');
  Query1.SQL.Add('FROM CUSTOMERS');
  Query1.SQL.Add('GROUP BY NAME,ADDRESS,CITY,SALARY');
  Query1.SQL.Add('ORDER BY NAME');
  Query1.Open;
  .....
end;

At the end of the procedure, the value of tot is always 0.
Can anyone tell me what's wrong?

 

Re:Problems with SUM in SQL statement


You are missing:

      tot := Query1.FieldByName('tot').Value;  // or tot := Query1['tot'];

immediately after the Open.

V/R
Russell L. Smith

Quote
Twins wrote in message <3617CCA7.2FCA8...@dimensione.com>...
>procedure SumRecords;
>var tot:double;
>begin
>  tot:=0;
>  Query1.Close;
>  Query1.SQL.Clear;
>  Query1.SQL.Add('SELECT NAME,ADDRESS,CITY,SALARY,');
>  Query1.SQL.Add('SUM (SALARY) as tot');
>  Query1.SQL.Add('FROM CUSTOMERS');
>  Query1.SQL.Add('GROUP BY NAME,ADDRESS,CITY,SALARY');
>  Query1.SQL.Add('ORDER BY NAME');
>  Query1.Open;
>  .....
>end;

Other Threads