回答編集履歴
1
追記
answer
CHANGED
@@ -40,4 +40,64 @@
|
|
40
40
|
,if(isnull(q1),1,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name)) as ratio
|
41
41
|
from `check` as t1 group by name,q1 with rollup
|
42
42
|
having name='aaaa' and q1='y'
|
43
|
+
```
|
44
|
+
# 追記
|
45
|
+
- なんか変なので書き直し
|
46
|
+
|
47
|
+
```SQL
|
48
|
+
select name,q,yn,count(*) as count
|
49
|
+
,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name) as ratio
|
50
|
+
from (
|
51
|
+
select name,'q1' as q,q1 as yn from `check`
|
52
|
+
union all select name,'q2',q2 from `check`
|
53
|
+
union all select name,'q3',q3 from `check`
|
54
|
+
union all select name,'q4',q4 from `check`
|
55
|
+
) as t1
|
56
|
+
group by name,q,yn
|
57
|
+
```
|
58
|
+
|
59
|
+
- 上記集計して
|
60
|
+
```SQL
|
61
|
+
select name
|
62
|
+
,yn
|
63
|
+
,sum((q='q1')* ratio) as q1
|
64
|
+
,sum((q='q2')* ratio) as q2
|
65
|
+
,sum((q='q3')* ratio) as q3
|
66
|
+
,sum((q='q4')* ratio) as q4
|
67
|
+
from(
|
68
|
+
select name,q,yn,count(*) as count
|
69
|
+
,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name) as ratio
|
70
|
+
from (
|
71
|
+
select name,'q1' as q,q1 as yn from `check`
|
72
|
+
union all select name,'q2',q2 from `check`
|
73
|
+
union all select name,'q3',q3 from `check`
|
74
|
+
union all select name,'q4',q4 from `check`
|
75
|
+
) as t1
|
76
|
+
group by name,q,yn
|
77
|
+
) as t3
|
78
|
+
group by name,yn
|
79
|
+
```
|
80
|
+
|
81
|
+
- havingで条件をつけて抽出
|
82
|
+
```SQL
|
83
|
+
select name
|
84
|
+
,yn
|
85
|
+
,sum((q='q1')* ratio) as q1
|
86
|
+
,sum((q='q2')* ratio) as q2
|
87
|
+
,sum((q='q3')* ratio) as q3
|
88
|
+
,sum((q='q4')* ratio) as q4
|
89
|
+
from(
|
90
|
+
select name,q,yn,count(*) as count
|
91
|
+
,count(*)/(select count(*) from `check` as t2 where t1.name=t2.name) as ratio
|
92
|
+
from (
|
93
|
+
select name,'q1' as q,q1 as yn from `check`
|
94
|
+
union all select name,'q2',q2 from `check`
|
95
|
+
union all select name,'q3',q3 from `check`
|
96
|
+
union all select name,'q4',q4 from `check`
|
97
|
+
) as t1
|
98
|
+
group by name,q,yn
|
99
|
+
) as t3
|
100
|
+
group by name,yn
|
101
|
+
having name='aaaa' and yn='y'
|
102
|
+
|
43
103
|
```
|