すでに回答はでているようなのでwith rollupのサンプル
SQL
1create table `check`(name varchar(20),q1 varchar(10),q2 varchar(10),q3 varchar(10),q4 varchar(10));
2insert into `check` values
3('aaaa','y','n','y','y'),
4('bbbb','y','y','n','n'),
5('cccc','n','y','n','y'),
6('aaaa','n','n','n','y'),
7('bbbb','y','n','y','n'),
8('cccc','y','n','n','n'),
9('aaaa','n','n','n','n'),
10('bbbb','n','n','n','n'),
11('cccc','y','y','y','y');
SQL
1select name,q1,count(*) as count
2from `check` as t1 group by name,q1 with rollup
SQL
1select name
2,q1
3,count(*) as count
4,if(isnull(q1),1,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name)) as ratio
5from `check` as t1 group by name,q1 with rollup
6
7
SQL
1select name
2,q1
3,count(*) as count
4,if(isnull(q1),1,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name)) as ratio
5from `check` as t1 group by name,q1 with rollup
6having name='aaaa' and q1='y'
追記
SQL
1select name,q,yn,count(*) as count
2,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name) as ratio
3 from (
4select name,'q1' as q,q1 as yn from `check`
5union all select name,'q2',q2 from `check`
6union all select name,'q3',q3 from `check`
7union all select name,'q4',q4 from `check`
8) as t1
9group by name,q,yn
SQL
1select name
2,yn
3,sum((q='q1')* ratio) as q1
4,sum((q='q2')* ratio) as q2
5,sum((q='q3')* ratio) as q3
6,sum((q='q4')* ratio) as q4
7from(
8select name,q,yn,count(*) as count
9,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name) as ratio
10 from (
11select name,'q1' as q,q1 as yn from `check`
12union all select name,'q2',q2 from `check`
13union all select name,'q3',q3 from `check`
14union all select name,'q4',q4 from `check`
15) as t1
16group by name,q,yn
17) as t3
18group by name,yn
SQL
1select name
2,yn
3,sum((q='q1')* ratio) as q1
4,sum((q='q2')* ratio) as q2
5,sum((q='q3')* ratio) as q3
6,sum((q='q4')* ratio) as q4
7from(
8select name,q,yn,count(*) as count
9,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name) as ratio
10 from (
11select name,'q1' as q,q1 as yn from `check`
12union all select name,'q2',q2 from `check`
13union all select name,'q3',q3 from `check`
14union all select name,'q4',q4 from `check`
15) as t1
16group by name,q,yn
17) as t3
18group by name,yn
19having name='aaaa' and yn='y'
20