SomeUser
SomeUser

Reputation: 2041

How to select sum of column in result of select?

How to select sum of column in result of select?

Upvotes: 5

Views: 27704

Answers (5)

JamesH
JamesH

Reputation: 85

SELECT SUM(`subquery`.`qty`) FROM (
SELECT `qty` FROM `table1` WHERE [ your query conditions ]
) AS `subquery` 

Upvotes: 0

Fergal Moran
Fergal Moran

Reputation: 4634

SELECT SUM(col) FROM table

Upvotes: 1

Andreas Aarsland
Andreas Aarsland

Reputation: 955

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

Upvotes: 4

Filip Ekberg
Filip Ekberg

Reputation: 36287

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()

Upvotes: 15

Daniel Vassallo
Daniel Vassallo

Reputation: 344291

SELECT 
    SUM(your_column) AS Total
FROM
    your_table;

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

Upvotes: 17

Related Questions