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?? 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 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 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 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é. 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. 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. 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
Archived
This topic is now archived and is closed to further replies.