Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select sum of column in result of select?

Tags:

mysql

How to select sum of column in result of select?

like image 318
SomeUser Avatar asked Feb 17 '10 12:02

SomeUser


People also ask

How do I sum in SELECT query?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; The SELECT statement in SQL tells the computer to get data from the table.

How do you display the sum of a column in SQL?

AVG() Syntax The SUM() function returns the total sum of a numeric column.


4 Answers

SELECT 
    SUM(your_column) AS Total
FROM
    your_table;

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

like image 74
Daniel Vassallo Avatar answered Oct 15 '22 20:10

Daniel Vassallo


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

like image 24
Filip Ekberg Avatar answered Oct 15 '22 18:10

Filip Ekberg


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

like image 42
Andreas Aarsland Avatar answered Oct 15 '22 18:10

Andreas Aarsland


SELECT SUM(col) FROM table

like image 39
Fergal Moran Avatar answered Oct 15 '22 18:10

Fergal Moran