By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,405 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,405 IT Pros & Developers. It's quick & easy.

adding SUM to query

P: n/a
I have a SELECT * query that returns info such as 'name', 'date',
'cost', and 'notes'. At the head of this query return it reads how
many records were found. I would like to append that with 'how many
records were found at a cost of' ...

But haven't been able to find a means of totalizing the 'cost' column
in my query. Does anyone have any ideas on how this might be
accomplished ??? I've spent considerable time on the net looking at
examples and my reference book but so far, no luck.

TIA,
Chris
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chris (co**********@yahoo.com) wrote:
: I have a SELECT * query that returns info such as 'name', 'date',
: 'cost', and 'notes'. At the head of this query return it reads how
: many records were found. I would like to append that with 'how many
: records were found at a cost of' ...

: But haven't been able to find a means of totalizing the 'cost' column
: in my query. Does anyone have any ideas on how this might be
: accomplished ??? I've spent considerable time on the net looking at
: examples and my reference book but so far, no luck.

The row count you see is extra information, it is not part of the query,
and what sort of information you can get depends on the application you
are using. Something like mysql will give you basic feedback such as the
row count. If you used a report application then you could ask the report
application to provide a sum at the end, but otherwise you have to
calculate it your self. If you used a report application then it would
simply be doing something like what I describe below.
There are two basic ways to do it your self

-1- do the sum yourself as you scan through the rows
(pseudo code, based on some mysql/php examples I have)

$sum = 0;
while($row = mysql_fetch_array($sth))
{
$sum += $row[cost];
Print_the_row($row);
}
Print_the_cost($sum);
-2- or use two queries
mysql> select * from query;
mysql> select sum(cost) from query;

--

This space not for rent.
Jul 17 '05 #2

P: n/a
On 28 May 2005 13:45:10 -0800, Malcolm Dew-Jones wrote:
$sum += $row[cost];


Array indices are numeric or string, not constant. It works because it
used to work in ancient php, but you should really say:

$sum += $row['cost'];
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3

P: n/a
Ewoud Dronkert (fi*******@lastname.net.invalid) wrote:
: On 28 May 2005 13:45:10 -0800, Malcolm Dew-Jones wrote:
: > $sum += $row[cost];

: Array indices are numeric or string, not constant. It works because it
: used to work in ancient php, but you should really say:

: $sum += $row['cost'];
good point thanks.

--

This space not for rent.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.