473,396 Members | 1,895 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.

Grand Totals?

I'm using GROUP BY to come up with totals for some items, but would like to either have a grand total at the bottom, or a running total field.

I tried to use a variable as I found in several places on the web:
Expand|Select|Wrap|Line Numbers
  1. SELECT @total := total + value AS `Running Total`
but what I'm totalling is an aggregate, and it doesn't seem to work. I tried to explore some options dev.mysql.com, but again, my field seems to be too complex.

More specifically, I have a series of harvest records. Each harvest records records a product ID and a weight. A product table lists names for the products; a product value lists prices per weight.

The query sums weights for each product, then multiplies that by the price per weight. So now I have a table with the sum of the prices for each product, but I'm unable to then sum all those sums to get a grand total.

Any thoughts?

Here is the SQL I'm using to build the per-product sums. The `RT` column yields NULLS in this particular attempt:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     product AS ID,
  3.     MAX(s_product.name) AS Name,
  4.     resource AS Resource,
  5.     CONCAT(sum(quantity),' ',min(harvest.units)) AS Quantity,
  6.     CONCAT('$', ROUND((SUM(quantity) * prices.price), 2),' ',prices.market_type) AS Value,
  7.     @rt := @rt + (ROUND((SUM(quantity) * prices.price), 2)) AS RT,
  8.     COUNT(*) AS Harvests,
  9.     MIN(date) AS Begin,
  10.     MAX(date) AS End,
  11.     notes AS Notes
  12. FROM
  13.     s_product_harvest harvest
  14.   INNER JOIN
  15.     s_product on s_product.ID = harvest.product
  16.   LEFT OUTER JOIN
  17.     s_product_market_prices prices ON ID = prices.product_ID
  18. WHERE
  19.     date >= '2009-01-01'
  20.   AND
  21.     date <= '2009-12-31 23:59:59'
  22. GROUP BY
  23.     product
  24. ORDER BY
  25.     s_product.name
You can see an example of the working query (without the attempt at a running total) here:
http://www.EcoReality.org/wiki/2009_harvest

Thanks for any advice or ideas offered!
Nov 24 '09 #1
2 3010
mwasif
802 Expert 512MB
Did you try ROLLUP?
Nov 24 '09 #2
Thanks for the pointer, but that won't work with my ORDER BY clause, as stated on the page you linked.

I did try it and verified that it doesn't like my ORDER BY. Since the GROUP column is an opaque reference (INT), I really need to sort on the real names of things, not their arbitrary INT keys.

Also, when I took out the ORDER BY, I got strange results. It summarized the "wrong" column, seemingly giving me a total for the weights, which are a mix of grams, kilograms, and "each," so it's totally useless to sum kilograms of pears with grams of bok choy with numbers of eggs!

I'm going to play with this a bit more, but in the mean time, any other ideas?
Nov 25 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: CSDunn | last post by:
Hello, I have 14 fields on a report that hold integer values. The field names use the following naming convention: T1Number, T2Number ....T14Number. I need to get a 'sub total' of all fields as...
2
by: Darren | last post by:
Hi, I am trying to write an Aged Debtors report in MSquery, I have created all of the columns sucessfully, I just need to have totals of each column at the bottom of the report - how can this be...
4
by: Coleen | last post by:
Hi All :-) Cor & David, Thank you for your help, sorry I did not reply before now, but for some reason my Newsgroup Reader (Outlook) does not show your last posts. The last one I got from Cor...
6
by: Coleen | last post by:
Hi All :-) Thanks for all of your help Cor :-) I can not get the code you sent me to work in my application. I'm using an aspx datagrid in a web form. I'm getting the following error message...
0
by: jessicaeatworld | last post by:
Hi, I'm using Access 2003 on Windows XP. I have created a PivotTable View Form and at the bottom is an automatically inserted Grand Total row. I added sum and avg fields and then hid the details so...
1
by: smacky61 | last post by:
How do I summarize a report by finding the totals fees collected for each event and the grand total of all fees. The name of the report is Registered Attendees Report.
1
by: dan.cawthorne | last post by:
Kinda Confused to why i cant get a single text field to add up all the Projects that are filtered Via a Query. I Have a Query Based on a Projects Table. The Query Have a Criteria To Select a...
4
by: Micheal | last post by:
Greetings Access Group, Being relatively new to Access, I try to work through problems on my own and have been very successful, although I have a conundrum that I have been working on for two days...
15
klarae99
by: klarae99 | last post by:
I am working on an Inventory Database in Access 2003. I am working on a report that I could print when its time to file our State Sales Tax paperwork. The figures I need for this report are Total...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.