简体   繁体   中英

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 数据库

This is sql code that I have tried which isnt working

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM