Calculating percentage within a group

May be too late, but for upcoming searchers, possible solution could be:

select sex, employed, COUNT(*) / CAST( SUM(count(*)) over (partition by sex) as float)
  from my_table
 group by sex, employed

By IO Statistics this seems to be most effective solution – may be dependant on number of rows to be queried – tested on numbers above …

The same attitude could be used for getting male / female percentage:

select sex, COUNT(*) / CAST( SUM(count(*)) over () as float)
  from my_table
 group by sex

Regards,
Jan

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)