473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

adding SUM to query

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
3 1602
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tryfon Gavriel | last post by:
Hi all I recently noticed when trying to optimise a major query of a chess website I am the webmaster of, that adding an order by for "gamenumber" which is a clustered index field as in for...
3
by: GL | last post by:
Hi, Is there a way to add a field to an existing table using a query of some sort (without needing to manually add a field to the table). I know how to do it with a make table query, but I have...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
6
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
4
by: John Siracusa | last post by:
I recently had a problem where a bunch of postgres backends were taking up huge amounts of CPU time. I found a bunch of log messages like this: NOTICE: adding missing FROM-clause entry for...
6
by: ljungers | last post by:
Hi to all and hope someone may have an answer for me. I have a Form named Cust_lukup_Form that has 3 text boxes and a click button that uses a OnClick to call Cust_lukup_Macro that runs an...
2
by: dympna | last post by:
Hi can anyone suggest a fix for this... as I am a novice in access. I have created a training table with the following fields Employee Name - joe Training Received - Fork lift Date Received...
0
by: uno7031 | last post by:
Help Please!!! Adding 5 Days to another Date in an access query Good Morning, Help please…. I am new to access and trying to write a query that will add 5 days between a RecDate and a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.