我想计算每个会员类型的当选人百分比, 我能够做到这一点:
SELECT Member, COUNT(Name) * 100 /
(SELECT COUNT(*) FROM 'results' WHERE Member= '1' ) AS 'perc'
FROM 'results'
WHERE elected='yes' AND Member = '1'
但我想为所有成员值(也是'0')
执行此操作我的表格如下:
Name member Elected
----------------------------
joe 0 yes
ann 1 no
jef 0 no
这是我想要的结果:
Member Percentage
---------------------
0 50%
1 0%
答案 0 :(得分:1)
你应该能够使用这样的东西:
select member,
coalesce(count(case when elected = 'yes' then 1 end)/count(*), 0) * 100 Percentage
from results
group by member
结果:
| MEMBER | PERCENTAGE |
-----------------------
| 0 | 50 |
| 1 | 0 |
答案 1 :(得分:1)
SELECT Member,
Sum(Case When elected='yes' Then 1
Else 0 End) / COUNT(*) 'perc'
FROM 'results'
group by Member
答案 2 :(得分:0)
您的查询应该是
SELECT Member,
(COUNT(Case When elected='yes' Then 1 Else NULL End) / COUNT(*)) * 100 'perc'
FROM 'results'
group by Member
答案 3 :(得分:0)
尝试使用 Group By Member
如果您使用sqlfiddle.com,那么很容易为您提供帮助。
从 bluefeet 偷窃你走了
select member,
concat(FLOOR(coalesce(count(case when elected = 'yes' then 1 end)/count(*), 0) * 100), '%') Percentage
from results
group by member