Adding column value for same categories in different rows mysql sqlalchemy

Jay Patel :

I would like to get sum of totalExGst of description with the value of same category. e.g. Advertising Funds = 970.86+300.5+(-5.18)

I am developing invoice and bank transaction processing online tool where I can assign category to all transactions and descriptions which can be later on combine according to category to get total and generate profit and loss. In attached screen shot MySQL database screen where description is removed for privacy reason.

enter image description here

André Pletschette :

This query should do it:

SELECT description, sum(totalExGst) as Total
FROM myTable
GROUP BY description;

But to be honest, I don't find this a good table design. I suggest doing a new table, example "types" defining the possible descriptions, then doing something like this:

SELECT types.description, sum(myTable.totalExGst) as Total
FROM myTable
LEFT JOIN types on myTable.typeid = types.id
GROUP BY types.description;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=395262&siteId=1