Puoi provare un'istruzione sql come questa per SOMMA i valori GROUPing per colonna:
SELECT SUM(value) as Total_val, category FROM tbl_transaction GROUP BY category
E puoi usarlo in PHP in questo modo:(Digiterò con PDO perché i comandi mysql_ stanno invecchiando in questo momento e non sono consigliati)
//Creating connection
$connection=new PDO("mysql:host=127.0.0.1;dbname=database_name","root","");
//Creating main query, fetching main categories
$query=$connection->prepare("SELECT category, SUM(value) as MAIN_CAT_TOTAL_VAL FROM tbl_transaction GROUP BY category");
$query->execute(); //executing query
foreach($query->fetchAll() as $q) //fetching results
{
echo "<b>".$q["category"].":<br/></b>"; //printing main category name
//creating second query which sums value column and groups by sub category
$query2=$connection->prepare("SELECT SUM(value) as TOTAL_VALUE, sub-category FROM tbl_transaction WHERE category=:p1 GROUP BY sub-category");
$query2->bindParam(":p1",$q["category"],PDO::PARAM_STR); //binding parameter to second query
$query2->execute(); //executing second query
foreach($query2->fetchAll() as $q2) //fetching the results
{
echo $q2["sub-category"]." -> ".$q2["TOTAL_VALUE"]."<br/>"; //printing the results
}
echo "total amount of category ".$q["category"]." = ".$q["MAIN_CAT_TOTAL_VAL"];
echo "<hr/>"; //cosmetics
}
Questo dovrebbe funzionare se lo modifichi correttamente. Può sembrare un po' complicato se non sei sicuro di come usare PDO, ti consiglio di dargli un'occhiata.