I have three tables: applications, permissions and application_permissions
|------------| |------------------------| |-----------|
|applications| |applications_permissions| |permissions|
|------------| |------------------------| |-----------|
| id | <-| application_id | | id |
| price | | permission_id |-> | name |
|------------| |------------------------| |-----------|
For applications, there are two categories: free and commercial (price = '0' and price! = '0')
Now I would like to find out for each permission how many percent of the total number of applications refer to it; And this is for both categories
Free:
id, percentage
1 , 20.0230
2 , 0.0000
3 , 0.0312
...
Commercial:
id, percentage
1 , 18.0460
2 , 0.0000
3 , 0.0402
...
I developed the following query, but it does not include rights identifiers without an application: /
SELECT (SELECT name FROM permissions WHERE id = applications_permissions.permission_id) AS "name",
100::float * COUNT(*)/(SELECT COUNT(name) FROM applications WHERE price = \'0\') AS "percent"
FROM applications, applications_permissions
WHERE applications.id = applications_permissions.application_id
AND applications.price = \'0\'
GROUP BY applications_permissions.permission_id
ORDER BY percent DESC')
How should I do it? I tried for several hours already (this request, different JOINs), but it eludes me: /
source
share