Reputation: 21
I'm trying to calculate a percentage of customers that live within a specified range (<=10miles) from the total number of customers, the data comes from the same table.
Customer_Loc (table name)
Cust_ID | Rd_dist (column names)
I've tried the query below which returns syntax errors.
select count(Rd_dist) / count (Cust_ID)
from customer_loc
where Rd_Dist <=10 *100 as percentage
I realise the solution to this may be fairly easy but I'm new to SQL and it's had me stuck for ages.
Upvotes: 2
Views: 1612
Reputation: 1270091
The problem with your query is that you are filtering out all the customers who are more than 10 miles away. You need conditional aggregation, and this is very easy in MySQL:
select (sum(Rd_Dist <= 10) / count(*)) * 100 as percentage
from customer_loc;
Upvotes: 1