Fixes sql select for distances.