Jump to content

Help with IF statement


nealios

Recommended Posts

Hello,

 

Can some one help me with the following problem?

 

The first two lines calculate sucessfully, the first totals the column, the second calculates the vat. The next should add the two together but i cant get the correct figure!

 

Any pointers?

 

SELECT 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+amount * 0.175 ,2), amount)) AS toolsnet, 

 

Many thanks

Link to comment
Share on other sites

my data

[pre]

id | amount | vatid

---+--------+-------

1  |  100  |  2

2  |  200  |  2

3  |  300  |  0

[/pre]

 

my query

 

SELECT SUM(ROUND(amount,2)) AS toolstotal,

SUM(IF(vatid='2',ROUND(amount * 0.175, 2), 0.00)) AS toolsvat,

SUM(IF(vatid='2',ROUND(amount * 1.175 ,2), amount)) AS toolsnet

FROM mytable

 

my results

[pre]

toolstotal |  toolsvat  |  toolsnet

-----------+------------+--------------

600.00    |  52.50    |  652.50

Link to comment
Share on other sites

 

  Expense Type    Amount    VAT        NET

                    Tools      100.00    17.50    117.50

    Tools     100.00 17.50   117.50

    Tools     100.00 17.50   117.50

    Tools     100.00 17.50   117.50

    Tools     100.00 0.00   100.00

 

I have five columns. Where the vat id is selected to '2' it calculates the vat, where it is selected to '1' no vat is necessary. I dont know why mine wont work?

 

Results

 

    Total  Vat  Net

Tools 500.00 70.00 1320.00

 

"SELECT 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

Link to comment
Share on other sites

Yes there is a join.

 

I have a page which i can enter two dates and then the query searches for all records between those two dates.

 

SELECT 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), 0.00)) AS toolsnet
FROM expenditure, expenses WHERE expenditure.expenseid = expenses.expenseid AND `extransactiondate` BETWEEN '" . $date1 . "' AND '" . $date2 . "'";

 

many thanks

Link to comment
Share on other sites

Thanks for the link, i had a look and i implemented the group by function. This enabled the calculation to work. I dont think the left join is relevant as i am only using the join to find the expensetype from the expenseid rather than taking amounts from both tables.

 

$sql1 = "
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, expenses WHERE expenditure.expenseid = expenses.expenseid AND`extransactiondate` BETWEEN '" . $date1 . "' AND '" . $date2 . "'
GROUP BY amount";

 

 

However when i add a couple of more lines it only calculates the first three. Ive tried group by expensetype but it only calculates the second set 'petrol'. I am miffed!

 

$sql1 = "
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, expenses WHERE expenditure.expenseid = expenses.expenseid AND`extransactiondate` BETWEEN '" . $date1 . "' AND '" . $date2 . "'
GROUP BY amount";

Link to comment
Share on other sites

Ive completly got rid of the join. I can calculate the first two column total and vat of both expenseid 3 and 4.

 

However the net is still not calculating properly. I think the syntax of the statement maybe wrong but i have nothing to compare it to. Does anyone have an alternative methods or anything they think i should try?

 

SELECT SUM(IF(expenseid ='3', ROUND(amount,2), 0.00)) AS toolstotal, 
SUM(IF(expenseid ='3' AND vatid='2',ROUND(amount * 0.175, 2), 0.00)) AS toolsvat,
SUM(IF(expenseid ='3' AND vatid='2',ROUND(amount * 1.175 ,2), amount)) AS toolsnet,

SUM(IF(expenseid ='4', ROUND(amount,2), 0.00)) AS petroltotal, 
SUM(IF(expenseid ='4' AND vatid='2',ROUND(amount * 0.175, 2), 0.00)) AS petrolvat,
SUM(IF(expenseid ='4' AND vatid = '2', ROUND(amount * 1.175, 2), amount)) AS petrolnet

FROM expenditure WHERE `extransactiondate` BETWEEN '" . $date1 . "' AND '" . $date2 . "'";

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.