count cos distance in sql query