tags:

views:

33

answers:

5

Hi i have 2 fields in database and i would like to find the difference of them in percentage. So mathematical the formula should be

ABSOLUTE((field1-field2)/(MAXIMUM(field1, field2)))

the problem is I dont know how to ask for maximum of 2 numbers. Since MAX in sql returns the max of column.

A: 

maybe someting like:

if (field1 > field2, (field1 - field2)/field1, (field2 - field1) / field1)
webclimber
+1  A: 
SELECT  ABS(field1 - field2) / GREATEST(field1, field2)
FROM    mytable
Quassnoi
Thank you very much!You solved my problem!
Granit
A: 

How about simply this

ABS((field1-field2)/(GREATEST(field1, field2)))
Paul Dixon
A: 

Try an IF:

SELECT IF(field1 < field2, field1, field2).

Seth
A: 

You are looking for the function named GREATEST, since MAX is used elsewhere in SQL.

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest

Anax