473,324 Members | 2,178 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,324 software developers and data experts.

Novice trying to SUM array elements from query results.

Mo
I thought that this was going to be super easy (and it probably is),
but I couldn't figure it out.
I have results from a MySQL query which include PartNumber, Qty, Cost,
and Price.
In one part of the report, I want to only display Sum/Total info.

As an example, let's say there are 5 rows of data, and I want to know
only the sum total of $result["qty"] from all 5 rows.

I was expecting to use array_sum(), but have come to discover that
this only sums all the elements of a single array.

Any pointers?
~Mo
Jun 27 '08 #1
3 2016
..oO(Mo)
>I thought that this was going to be super easy (and it probably is),
but I couldn't figure it out.
I have results from a MySQL query which include PartNumber, Qty, Cost,
and Price.
In one part of the report, I want to only display Sum/Total info.

As an example, let's say there are 5 rows of data, and I want to know
only the sum total of $result["qty"] from all 5 rows.

I was expecting to use array_sum(), but have come to discover that
this only sums all the elements of a single array.

Any pointers?
I would let the database do it. Who cares about a simple second query to
get the totals? I doubt that this would cause any performance problem.

Micha
Jun 27 '08 #2
Mo
On Jun 13, 12:27 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Mo)
I thought that this was going to be super easy (and it probably is),
but I couldn't figure it out.
I have results from a MySQL query which include PartNumber, Qty, Cost,
and Price.
In one part of the report, I want to only display Sum/Total info.
As an example, let's say there are 5 rows of data, and I want to know
only the sum total of $result["qty"] from all 5 rows.
I was expecting to use array_sum(), but have come to discover that
this only sums all the elements of a single array.
Any pointers?

I would let the database do it. Who cares about a simple second query to
get the totals? I doubt that this would cause any performance problem.

Micha
There are two reasons why I want to do it in my report coding:
1) Some of these values are currently stored in the DB, but
unfortunately, they cannot be depended upon to be accurate or up to
date.
Until we can rebuild the code and eliminate those bugs, I'd like to be
able to get reliable info.
2) It would be a good learning experience.

You mentioned a second query.
Is that your suggestion?
Just run a whole new query instead of extracting the info from the
existing dataset?
Is there a more efficient way to do this?

~Mo
Jun 27 '08 #3
Mo wrote:
On Jun 13, 12:27 pm, Michael Fesser <neti...@gmx.dewrote:
>.oO(Mo)
>I thought that this was going to be super easy (and it probably is),
but I couldn't figure it out.
I have results from a MySQL query which include PartNumber, Qty,
Cost, and Price.
In one part of the report, I want to only display Sum/Total info.
>As an example, let's say there are 5 rows of data, and I want to
know only the sum total of $result["qty"] from all 5 rows.
>I was expecting to use array_sum(), but have come to discover that
this only sums all the elements of a single array.
>Any pointers?

I would let the database do it. Who cares about a simple second
query to get the totals? I doubt that this would cause any
performance problem.

Micha

There are two reasons why I want to do it in my report coding:
1) Some of these values are currently stored in the DB, but
unfortunately, they cannot be depended upon to be accurate or up to
date.
If you are getting the data from the db and you cannot rely on them, then
why does summing them outside of the SQL make them any more accurate?
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
1
by: gretchen.ogrady | last post by:
I admit - I'm a simple user but looking to improve skills. Instructions aren't helping and have searched this group but am getting bogged down by some of the programming-speak. I have a query...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
20
by: John Mark Howell | last post by:
I had a customer call about some C# code they had put together that was handling some large arrays. The performance was rather poor. The C# code runs in about 22 seconds and the equivalent...
6
by: loga123 | last post by:
I am using asp.net 2.0. I want to get the results of a sql query into an array at run-time. Number of records returned varies each time.....so I want to declare the dimensions of array runtime...
3
by: Kevin Chambers | last post by:
Hi all-- Quick question: has anyone come up with an easy way to take an array and use its elements as part of a WHERE clause? For example: <This obviously doesn't work> SELECT * FROM Table1...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
5
by: admin | last post by:
I have a class that has a function that should query a database and return a list of usernames as well as their id. What type should I use as a return type, that can hold data such as: user1, 1...
2
by: Akhenaten | last post by:
I have the following query I run to pull some data from the db. The sql (when run on the db) returns 2 elements. However, if I var dump my variable ($aidlook) then the first element in the array...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.