Question : Closest number match

Hi,

I have a table with varying distance ratios in it and I want to write a query to find the row that closely matches a given value.

e.g. Table DistancesRatios contains Fields: ID and Distance

ID is autonumber
Distance contains 1,2,3,4,5,6,10,20,30,40,500,1000.

The distance calculation on the form returns a value e.g. 11 so I want to say that DistanceRatio is 10 (closest number) and return the DistanceRatio ID.

I could probably do this by traversing a recordset and comparing the ratios greater or less than the value supplied but if there's a better way I'll take it.




Answer : Closest number match

Hi geraintcollins,

Select Top 1 Distance from DistanceRatios order by abs(11-distance) asc

Should work, essentially the order by will get the difference between the two and give you the closest.

Tim Cottee
Random Solutions  
 
programming4us programming4us