運到 等
運到 等

Reputation: 151

SQL: How can I find the total and average value in one SQL statement?

I would like to calculate the total amount of expenditure and the calculate the average value from the number of rows like this:

SELECT AVG(SUM(expenditure)) from INCOME;

However, there is a error say "misuse of aggregate function sum()"

how can I achieve this?

Upvotes: 1

Views: 5988

Answers (2)

Mitch Wheat
Mitch Wheat

Reputation: 300559

You can't calculate the average of the total sum as there is only one total.

The average AVG() function already calculates the total as part of its logic.

This is what you want:

SELECT AVG(expenditure) as AverageExpenditure, 
       SUM(expenditure) as TotalExpenditure
from INCOME;

Upvotes: 3

blearn
blearn

Reputation: 1208

SELECT AVG(expenditure) AS avg_exp, SUM(expenditure) AS sum_exp FROM INCOME;

Upvotes: 2

Related Questions