I would like to show the percentage of gender on every row of the sql result. My data looks like:
CREATE TABLE Results
( employeeId varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL
, Employee_Name varchar(228) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL
, gender varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL
, Citizenship varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
insert into Results values
(1,'A','M','India'),
(2,'B','F','India'),
(4,'D','F','France'),
(3,'C','F','Lebanon'),
(5,'E','M','Sri Lanka');
The percentage by gender count would be M 40, F 60 by using
select gender, round(count(gender) * 100/sum(count(gender)) over (),2) percentage
from Results
group
by gender;
This query will return the result for M & F but i would like the percentage to be repeated for every row in the table,
employeeId EmployeeName gender Citizenship Percentage
1 A M India 40
2 B F India 60
4 D F France 60
3 C F Lebanon 60
5 E M Sri Lanka 40
How can i do this ?

There is no need for
group byor any joins.Use
COUNT()window function only:See the demo.
Results: