Jump to content

Please help! Sum problem


nealios

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.