Jump to content

Recommended Posts

Hello,

 

I am trying calculate vat on certain records in my table. At the moment i have the query half working and can perform my calculations where the vattype is set to yes.

 

But if the vattype is set to 'no' i want the vat alias to appear as NULL. I was thinking something like this but im not sure how to display it. 

 

IF(vattype='yes',ROUND(amount * 7/47, 2),amount), IF(vattype='no',ROUND(null) AS vat

 

My actual query

 

SELECT incomeid, intransactiondate, invoiceno, amount, IF(vattype='yes',ROUND(amount * 7/47, 2),amount)  AS vat, IF(vattype='yes',ROUND(amount-(amount* 7/47), 2),amount) AS net FROM `income`, `vat` WHERE income.vatid = vat.vatid;

 

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/85501-solved-help-with-if-select/
Share on other sites

You can't round(NULL). It can be zero or NULL, you pick. But since it a number I would say zero. So, you have to change the 'amount' column after the comma to what you want...example:

 

IF(vattype = 'yes', ROUND(amount * 7/47, 2), 0.00) AS vat

 

or

 

IF(vattype = 'yes', ROUND(amount * 7/47, 2), NULL) AS vat

 

 

Thanks for your reply i came over that part of the problem by

 

IF(vattype='no',ROUND(amount-(amount), 2),amount) AS vat,

 

 

However its still not doing exactly what i want the query to do. Im trying to display all the records in the table. If vattype is set to yes it should calculate vat on top.

If the vattype is set to no it only needs to display the record but it only needs to be displayed as blank.

 

At the moment the two if statements work individually but not together as below. I want to combine the first two if statements into one but not sure how it could be written.

 

$sql = "SELECT incomeid, intransactiondate, invoiceno, amount, 
IF(vattype='yes',ROUND(amount * 7/47, 2),amount) AS vat, 
IF(vattype='no',ROUND(amount-(amount), 2),amount) AS vat, 
IF(vattype='yes',ROUND(amount-(amount* 7/47), 2),amount) AS net 

FROM `income`, `vat` WHERE income.vatid = vat.vatid";

You're making it more difficult than it needs to be. You have: amount - (amount) which is zero. So, just do what I already showed in my previous post.

 

If you don't want to display something when it's zero, then change your query to only retrieve vat's that's greater than (or not) zero (also can use PHP to determine to display something or not).

 

Im now trying to add up the total amount of each table column. But only the first line works.

 

Can anyone solve this problem?

 

SELECT Round(SUM(amount),2) as total, 
IF(vattype = 'yes', ROUND(sum(amount * 7/47, 2), 0.00) AS totalvat,
IF(vattype='yes',ROUND(sum(amount-(amount* 7/47), 2),amount) AS totalnet 


FROM `income`, `vat` WHERE income.vatid = vat.vatid AND `intransactiondate` BETWEEN '" . $date1 . "' AND '" . $date2 . "' order by $sort";

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.