Reputation: 5
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
This is sql code that I have tried which isnt working
select * , SUM(TransactionAmount)
from UserBudgetTransaction
GROUP BY CategoryID
Upvotes: 0
Views: 75
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
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