I seek your help in generating a summary query result as %, n (numerator), and N (Denominator) out of a variable in the dataset for each underlying category:
e.g: data
| sr.no. | is_healthy | 
|---|---|
| 1 | yes | 
| 2 | no | 
| 3 | yes | 
| 4 | no | 
| 5 | yes | 
| 6 | yes | 
| 7 | no | 
| 8 | yes | 
| 9 | no | 
| 10 | yes | 
I expect the following as a summary result
| is_healthy | % | n | N | 
|---|---|---|---|
| yes | 60 | 6 | 10 | 
| no | 40 | 4 | 10 | 
What could be a possible SQL query to solve this challenge? Thanks in adavance.
Advertisement
Answer
N is the total count of rows in the table.
n is the count of rows matching a value.
% is just n/N*100.
You can get n by using count(*) in a grouped query and N by using count(*) in an ungrouped query. Since you know N will be a single row, you can cross-join on it:
SELECT     is_healthy, cnt, total, cnt / total * 100 AS percent
FROM       (SELECT   is_healthy, COUNT(*) AS cnt
            FROM     my_table
            GROUP BY is_healthy) grouped
CROSS JOIN (SELECT COUNT(*) AS total
            FROM   mytable) ungrouped