Board index » delphi » Elementary database enquiry : Select sum-waht then

Elementary database enquiry : Select sum-waht then

Relatively new to SQL and databases, I want to be able to sum the
values in fielld, and insert the result into another field in another
table. I cand do the select sum as in:

Query2.Sql.Add('Select Sum (Stock.Number_bought) from stock') ;

and this runs without problem. What do I do then?

Any help appreciated.

Alan Hale

 

Re:Elementary database enquiry : Select sum-waht then


It executes ok, but it does not execute the query. Just add:
  Query2.Open;
  TheSum := Query2.Fields[0].AsFloat;
  Query2.Close;

--
-------------------------
Regards,
Robert

Remove leading aa when replying.
MailTo: aarobert.ce...@eunet.si

NO QUESTIONS VIA EMAIL. IF YOU WANT ADDITIONAL INFO, ASK HERE.

Quote
Alan Hale wrote in message <365f0b38.1270...@news.clara.net>...
>Relatively new to SQL and databases, I want to be able to sum the
>values in fielld, and insert the result into another field in another
>table. I cand do the select sum as in:

>Query2.Sql.Add('Select Sum (Stock.Number_bought) from stock') ;

>and this runs without problem. What do I do then?

>Any help appreciated.

>Alan Hale

Re:Elementary database enquiry : Select sum-waht then


Quote
Alan Hale wrote in message <365f0b38.1270...@news.clara.net>...
>Relatively new to SQL and databases, I want to be able to sum the
>values in fielld, and insert the result into another field in another
>table. I cand do the select sum as in:

>Query2.Sql.Add('Select Sum (Stock.Number_bought) from stock') ;

>and this runs without problem. What do I do then?

Assuming the Query is pointing at the correct DB Alias then

Query2.Open;   //execute the query
MySum := Query2.Fields[0].asInteger;  //Get value

Note,  if your DB supports it,  you can change the SQL to
Query2.Sql.Add('Select Sum (Stock.Number_bought) Number_bought from stock')
;

this will add an column alias and allow you to use:
MySum := Query2.FIeldByName('Number_bought').asInteger;

        Jeff

Other Threads