I am working on a vehicle trading website. I have table named vehicle with around 20 fields including make, model etc of the vehicle. I am writing a search query to retrieve all the cars in the system group by make along with their counts by make.
So my objective is to get all of vehicles in the system grouped by their make ALONG with their counts of the make but the counts part does not work correctly. It returns total number of cars by make ignoring my distance calculation criteria.
I am executing following following SQL:
SELECT * FROM (SELECT *,ROUND(((ACOS(SIN(51.4811109 * PI() / 180) * SIN(latitude * PI() / 180) + COS(51.4811109 * PI() / 180) * COS(latitude * PI() / 180) * COS((-0.433641 - longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515),2) AS distance, count(make) AS carcount FROM `vehicle` `t` WHERE (status='Active')) as v GROUP BY make HAVING distance<=10
It is returning everything correct except the carcount
for which it returns 325 BMW cars in the system(total BMW cars in the system) instead of 12 BMW cars(only 10 cars exist in 10 miles distance).
Can anyone see what I am doing wrong? Thanks for your help.