Board index » delphi » SELECT SUM

SELECT SUM

Hellody

Can you help me?  PLEASE?

I am learning Delphi 4 and SQL... I want to exec a query to return the
total of a selection of rows...eg:

SELECT SUM (budgets) sum_of_budgets
FROM project
WHERE ....

Is that the right syntax?  (to total a field called budgets).
Assuming it is correct, how do i return the sum_of_budgets into
a variable in my delphi code?

I hope that makes sense. What i want to do is very simple but my
explanation may be complex!!!

Cheers for any help.....

Jon

 

Re:SELECT SUM


Quote
>Hellody

>Can you help me?  PLEASE?

>I am learning Delphi 4 and SQL... I want to exec a query to return the
>total of a selection of rows...eg:

>SELECT SUM (budgets) sum_of_budgets
>FROM project
>WHERE ....

>Is that the right syntax?  (to total a field called budgets).
>Assuming it is correct, how do i return the sum_of_budgets into
>a variable in my delphi code?

>I hope that makes sense. What i want to do is very simple but my
>explanation may be complex!!!

>Cheers for any help.....

>Jon

Use a TQuery Object
Assign its DatabaseName property and then
in the SQL property wqrite your query
After that, click x 2 the component and you will see
a smallish window appear. Press Ins and you'll
see a list of fields. To add all of them press OK.
You have just created an object for each field.
ie. Query1 : TQuery, SUM_WHATEVER Field (integer) in the query
will get you a Query1SUM_WHATEVER : TIntegerFiled
you can use it like this:

Something := Query1SUM_WHATEVER.AsInteger;

  Kind regards,

     Konstantin V {*word*224}ov aka JHVHS
                                        jh...@quadrus.ru

  Quadrus-M   http://www.quadrus.ru

Re:SELECT SUM


Try this:

Set TQuery.SQL to

SELECT :MyResult = SUM (budgets)
FROM project
WHERE ....

Define MyResult as Output parameter.

In run time execute query (not open), after that read the output parameter
with ParamByName(...).Value

I Hope this helps,
tomi.

Quote
jj wrote in message <370B3F58....@conceptsystems.co.uk>...
>Hellody

>Can you help me?  PLEASE?

>I am learning Delphi 4 and SQL... I want to exec a query to return the
>total of a selection of rows...eg:

>SELECT SUM (budgets) sum_of_budgets
>FROM project
>WHERE ....

>Is that the right syntax?  (to total a field called budgets).
>Assuming it is correct, how do i return the sum_of_budgets into
>a variable in my delphi code?

>I hope that makes sense. What i want to do is very simple but my
>explanation may be complex!!!

>Cheers for any help.....

>Jon

Re:SELECT SUM


Your syntax looks right. What you have to do id use the Open method on the
query in order to get it to execute the SQL command and return a resultset
and then use the FieldByName property.
  e.g. Query1.Open;
         Variable1 := Query.FieldByName('sum_of_budgets').AsInteger;
Quote
jj wrote:
> Hellody

> Can you help me?  PLEASE?

> I am learning Delphi 4 and SQL... I want to exec a query to return the
> total of a selection of rows...eg:

> SELECT SUM (budgets) sum_of_budgets
> FROM project
> WHERE ....

> Is that the right syntax?  (to total a field called budgets).
> Assuming it is correct, how do i return the sum_of_budgets into
> a variable in my delphi code?

> I hope that makes sense. What i want to do is very simple but my
> explanation may be complex!!!

> Cheers for any help.....

> Jon

Other Threads