lopes_andre Posted October 24, 2008 Share Posted October 24, 2008 Hi, I need to do an Arithmetic operation in a query, but I don't know if it is possible to to this. I want to calculate the difference in % of two rows... For that I need to make 2 calculations: Salary June: 3555 Salary July: 3950 3950 - 3555 = 395 <-- Diference between the two months in value 3950/395 = 10 % <-- Diference between the tho months in % My question. It is possible to do this two operation in a query? Something like: Select (sal_july - sal_june = $X sal_july / $X = DIF_SAL ) as VAR_SAL from sal I Don't know if I have explained well, but I need to do two basic arithmetic operation in ( ( .... ) as FIELD_NAME ). This is possible?? Quote Link to comment https://forums.phpfreaks.com/topic/129940-solved-arithmetic-operation-in-a-query-possible/ Share on other sites More sharing options...
Barand Posted October 24, 2008 Share Posted October 24, 2008 are the two salaries in the same row or in different rows Quote Link to comment https://forums.phpfreaks.com/topic/129940-solved-arithmetic-operation-in-a-query-possible/#findComment-673627 Share on other sites More sharing options...
lopes_andre Posted October 24, 2008 Author Share Posted October 24, 2008 They are in the same row of different sub-querys. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/129940-solved-arithmetic-operation-in-a-query-possible/#findComment-673629 Share on other sites More sharing options...
mrmitch Posted October 24, 2008 Share Posted October 24, 2008 Using your formula, just move what you have for $X Select (sal_july / (sal_july - sal_june) ) as VAR_SAL from sal Quote Link to comment https://forums.phpfreaks.com/topic/129940-solved-arithmetic-operation-in-a-query-possible/#findComment-673711 Share on other sites More sharing options...
lopes_andre Posted October 24, 2008 Author Share Posted October 24, 2008 Hi, Thanks for your answer!! It works, but when there is 0% of difference gives me an error of division by zero. What solution should I implement to correct this? Best Regards, André. Quote Link to comment https://forums.phpfreaks.com/topic/129940-solved-arithmetic-operation-in-a-query-possible/#findComment-673750 Share on other sites More sharing options...
fenway Posted October 24, 2008 Share Posted October 24, 2008 Use an IF() to catch this. Quote Link to comment https://forums.phpfreaks.com/topic/129940-solved-arithmetic-operation-in-a-query-possible/#findComment-673784 Share on other sites More sharing options...
lopes_andre Posted October 24, 2008 Author Share Posted October 24, 2008 Hi, I have solved in this way, (case when (T1.LIQUIDO - T2.LIQUIDO) = 0 then null else ((T2.LIQUIDO*100)/T1.LIQUIDO)-100 end) as DIF_PERCENTUAL Best Regards. Quote Link to comment https://forums.phpfreaks.com/topic/129940-solved-arithmetic-operation-in-a-query-possible/#findComment-673799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.