create table t1 (id integer primary key not null auto_increment, gender char(1));
insert into t1 values(NULL, 'M');
insert into t1 values(NULL, 'F');
insert into t1 values(NULL, 'F');
insert into t1 values(NULL, 'F');
insert into t1 values(NULL, 'M');
create table t2 (user_id integer not null, timestamp datetime);
insert into t2 values (1, sysdate());
insert into t2 values (2, sysdate());
insert into t2 values (1, sysdate());
insert into t2 values (3, sysdate());
insert into t2 values (4, sysdate());
insert into t2 values (4, sysdate());
select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender;
gender dist_count percentage
F 3 60.00
M 1 20.00
select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender order by percentage;