nealios Posted February 1, 2008 Share Posted February 1, 2008 Hello, I am attempting to add together the total amount of the transactions where the field matches the expensetype. Then any field where the vatid is equal to '2' i want to calculate the vat on top. Then i want to add the two figures together. The following code works when i do it for one expense type (Tools). SELECT amount,SUM(IF(expensetype ='Tools', ROUND(amount,2), 0.00)) AS toolstotal, SUM(IF(expensetype ='Tools' AND vatid='2',ROUND(amount * 0.175, 2), 0.00)) AS toolsvat, SUM(IF(expensetype ='Tools' AND vatid='2',ROUND(amount * 1.175 ,2), amount)) AS toolsnet FROM expenditure WHERE`extransactiondate` BETWEEN '" . $date1 . "' AND '" . $date2 . "' GROUP BY amount"; However when i add in a second expense type in this case 'petrol' it doesnt calculate. I don't know why its not working? SELECT amount,SUM(IF(expensetype ='Tools', ROUND(amount,2), 0.00)) AS toolstotal, SUM(IF(expensetype ='Tools' AND vatid='2',ROUND(amount * 0.175, 2), 0.00)) AS toolsvat, SUM(IF(expensetype ='Tools' AND vatid='2',ROUND(amount * 1.175 ,2), amount)) AS toolsnet, SUM(IF(expensetype ='Petrol', ROUND(amount,2), 0.00)) AS petroltotal, SUM(IF(expensetype ='Petrol' AND vatid='2',ROUND(amount * 0.175, 2), 0.00)) AS petrolvat, SUM(IF(expensetype ='Petrol' AND vatid='2',ROUND(amount * 1.175 ,2), amount)) AS petrolnet FROM expenditure WHERE`extransactiondate` BETWEEN '" . $date1 . "' AND '" . $date2 . "' GROUP BY amount"; Many thanks Link to comment https://forums.phpfreaks.com/topic/88915-please-help-sum-problem/ Share on other sites More sharing options...
Illusion Posted February 1, 2008 Share Posted February 1, 2008 Your first query is working fine? ??? Link to comment https://forums.phpfreaks.com/topic/88915-please-help-sum-problem/#findComment-455502 Share on other sites More sharing options...
nealios Posted February 2, 2008 Author Share Posted February 2, 2008 Yeah it works, but as soon as i add another set of expense types its ruins the result of the first set and neither calculate properly. Im stumped! Link to comment https://forums.phpfreaks.com/topic/88915-please-help-sum-problem/#findComment-456046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.