I have a pre-defined 6 digit number, and an Entry model with a ‘number’ field (integer) that will have several thousand randomly generated entries per month. I need to find the best way to find the entry that is closest to the pre-defined 6 digit number.
It’s basically a competition. At the start of a the month a random 6 digit number is chosen and people get to enter and are given a random number – at the end of the month the person with the closest number wins.
Anyone know what is the most efficient way to find the closest entry?
I am using Postgres 9.2 and Rails 3.2.9
The winner will have absolute of the difference between the predefined_number and chosen_number closest to zero.
Just have to query the picked numbers ordered by abs(predefined_number – chosen_number). The first on the list is the winner.