mysql - Get percent from one column -
i want % of hard workers each company don't know how. example:
google -> 66%
microsoft -> 33%
based in db:
create table a_test (id int ,`business` varchar(25),`name` varchar(25) , `hard_worker` int); insert a_test (id, business, name, hard_worker) values (1, 'google', 'tom rudhot', '1'), (2, 'microsoft', 'thomas carpenter', '0'), (3, 'google', 'peter butcher', '1'), (4, 'microsoft', 'sookie page', '0'), (5, 'microsoft', 'roonie redneck', '1'), (6, 'google', 'robbyn stackhouse', '0');
try (fiddle):
select business, 100 * count(*) / (select count(*) a_test hard_worker = 1) a_test hard_worker = 1 group business
edit:
previous query returns percentage of hard workers of company vs. hard workers.
but seems want hard vs. workers per company. easier:
select business, 100 * sum(case when hard_worker = 1 1 end) -- hard workers / count(*) -- workers a_test t1 group business
based on 0/1 implementation might use
100 * sum(hard_worker) / count(*) -- workers
Comments
Post a Comment