Question:
sql query sum up one column?
peter
2012-07-10 05:52:48 UTC
hello, i hava an sql query which gives me out the volume of several glasses in several rows. now i want to add upp all the volumes and add them together . I also want to have the result in a new row below the rest of the query.
Thanks for any help!
Three answers:
Martin
2012-07-10 06:42:59 UTC
You use the mysql sum() function to add together column data.



Example:



SELECT sum(glassVolume) as volumes FROM table.



Since this function act's like a subquery, you cannot have it appear as a unique row at the end of another query. The simplest solution is to just have two queries, one after the other (which achieves more or less the same effect).



However, if you aim to use the data in some program, you will have to carry out two queries and handle them as separate datastructures.



Happy coding!
TheMadProfessor
2012-07-10 07:41:55 UTC
This is better handled by reporting software such as Crystal Reports, but one way to do it purely in SQL might be something like this:



SELECT glass, row, volume FROM

(SELECT glass, row, volume, 1 as rowOrder FROM someTable)

UNION

SELECT "Total", row, volSum FROM

(SELECT row, SUM(volume) AS volSum, 2 FROM someTable GROUP BY row)

ORDER BY row, rowOrder
aub
2016-10-16 04:12:29 UTC
it is been a pair years. From the errors, you the two mispelled a column. out of your code, i think of the errors is coming from (readingDate > '09/29/2007') AND (readingDate < '10/31/2007') and (high quality = 0) and billingCycleId = '3' What table is readingDate and billingCycleid and high quality in? could desire to their be different columns with that call?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...