gunit12121
gunit12121

Reputation: 5

SQL statement that will sum total up

Hi I am trying to get each category ID to be displayed only once with the total TransactionAmount summed up for each categoryID.

I will then display this in a grid or hopefully get it to be visually displayed in a chart.

Below is screenshot of database Database

This is sql code that I have tried which isnt working

select * , SUM(TransactionAmount) 
from UserBudgetTransaction 
GROUP BY CategoryID

Upvotes: 0

Views: 75

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270593

The proper syntax for the GROUP BY is:

select CategoryID, SUM(TransactionAmount) 
from UserBudgetTransaction 
group by CategoryID;

SELECT * is not generally appropriate with a GROUP BY (well there is one case where the GROUP BY columns include functional dependencies). In general, the only columns in the SELECT that are not arguments to aggregation functions should be the GROUP BY keys.

Upvotes: 3

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31991

The number of column in selection must also be in group by (for most dbms) , you have selected all column but put only CategoryID in group by thats the wrong ,so correct will be like below

select CategoryID, SUM(TransactionAmount) 
from UserBudgetTransaction 
GROUP BY CategoryID

Upvotes: 2

Related Questions