Wednesday, 15 July 2015

sql - Calculating percentage within a group -


A table is given for the following command:

  Select gender, Count (*) by sex from my_table group; Sex, employed, count (*) by sex, employed by my_table group;  

returns:

  sex | Count ------- + ------ Male | 1960 women 1801  

and:

  sex | Employed | Calculation --------- + ---------- + ------- Male | F | 1523 Men | T | 437 Women | F | 1491 Women | T | 310  

I am having difficulty writing a question which will calculate the planned percentage within each sex group. The output should look like this:

  Sex | Employed | Counting | Percent --------- + ---------- + -------- + ----------- Male | F | 1523 | 77.7% (1523/1960) Men | T | 437 | 22.3% (437/1960) Women | F | 1491 82.8% (14 9 1/1801) Women | T | 310 | 17.2% (310/1801)  

You can do this with a sub-selection Join and join one:

  select t1.sex, employed, count (*) as 'count', count (*) / t2. *) Has been sexually assaulted by my_Table Group) on T2 on T1. TeX = T 2.X Group by T.SEX, planned;  

I can not think of other approaches from the top of my head.


No comments:

Post a Comment