mysql - sql query to calculate percentage -
in table below, need find percentage of males in state 'nyc'.
select count(*) table state = 'nyc' , gender = 'male';
gives me count of males nyc. need % of males nyc 75%.
+-------+-------+--------+ | name | state | gender | +-------+-------+--------+ | jon | nyc | male | +-------+-------+--------+ | obama | la | male | +-------+-------+--------+ | kat | nyc | male | +-------+-------+--------+ | andy | nyc | male | +-------+-------+--------+ | sri | nyc | female | +-------+-------+--------+
desired output:
state, male_percentage--> nyc, 75 %
as 3 males , 1 female total 4 nyc.
with 1 select table )
select state ,100 * sum( if(gender = 'male',1,0) ) / count(*) table state = 'nyc' group state
Comments
Post a Comment