Reputation: 387
I know this is a ridiculously obvious question, but I'm not getting the way SQL queries need to be combines. I have a table named Customers and I need to calculate the percentage of total customers that have the value x as 99. There are only three columns in the table.
So far I've got SELECT COUNT (*) FROM customer WHERE x=99
and SELECT COUNT (*) FROM customer
, but I'm not sure how to divide one by the other in a single query. I have tried SELECT (COUNT (*) FROM t_customer))/(Count (*) FROM t_customer WHERE customer_x=99))
but obviously I'm doing it very wrong! I'm using SQLite3. Any help would be much appreciated
Upvotes: 0
Views: 2458
Reputation: 1270513
My preferred method is:
SELECT AVG(case when x = 99 then 1.0 else 0 end)
FROM customer;
I believe the following also works:
SELECT AVG(x = 99)
FROM customer ;
Upvotes: 3