foreign keys - How to calculate percentage for each type in SQL -


i did digging couldn't find question similar mine...

i want make function calculates percentage of people specific education. example off possible educations (in table) how many pilots how many engineers, etc.

i have 2 tables: table users has foreign key table education. goal return table in have stated possible educations , percentage of people it.

i got numbers with

select educations.education, count(users.ideducation) numberofeducations educations left join users on (educations.ideducation= users.ideducation) group educations.education; 

and number of possible educations with

select count(ideducation) alltypes educations; 

now need , failed on fronts calculate each educations percentage , output in form of:

| education | percentage | | pilot     |     5%     | | engineer  |    10%     | | sales     |    25%     | | banker    |     8%     | 

you can use ansi standard window functions in databases:

select e.education, count(u.ideducation) numberofeducations,        count(u.ideducation)*1.0 / sum(count(u.ideducation)) on () ratio educations e left join      users u      on e.ideducation = u.ideducation) group e.education; 

the * 1.0 because databases integer division -- 1/2 0 rather 0.5.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -