tags:

views:

72

answers:

5

How to select sum of column in result of select?

A: 

SELECT SUM(col) FROM table

Alex DeLarge
This will never work, grouping by the col you are aggregating?
astander
Doh! Of course...
Alex DeLarge
+1  A: 
SELECT 
    SUM(your_column) AS Total
FROM
    your_table;

... or is this a trick question? :)

Daniel Vassallo
A: 

SELECT SUM(columnName) FROM Table GROUP BY columnName2

Leom Burke
This will never work, grouping by the col you are aggregating?
astander
Quite right - slow down when typing in future :) - now fixed
Leom Burke
+1  A: 

SELECT SUM(sales) as "Revenue" FROM details;

Andi
+3  A: 

Here is a list of MySQL built in functions for the group by aggregate

AVG() Return the average value of the argument
BIT_AND() Return bitwise and
BIT_OR()  Return bitwise or
BIT_XOR()(v4.1.1) Return bitwise xor
COUNT(DISTINCT)   Return the count of a number of different values
COUNT()   Return a count of the number of rows returned
GROUP_CONCAT()(v4.1)  Return a concatenated string
MAX() Return the maximum value
MIN() Return the minimum value
STD() Return the population standard deviation
STDDEV_POP()(v5.0.3)  Return the population standard deviation
STDDEV_SAMP()(v5.0.3) Return the sample standard deviation
STDDEV()  Return the population standard deviation
SUM() Return the sum
VAR_POP()(v5.0.3) Return the population standard variance
VAR_SAMP()(v5.0.3)    Return the sample variance
VARIANCE()(v4.1)  Return the population standard variance

I believe that you want SUM()

Filip Ekberg