gpt4 book ai didi

mysql - sql查询计算百分比

转载 作者:行者123 更新时间:2023-11-29 07:33:35 26 4
gpt4 key购买 nike

在下表中,我需要找到“纽约”州的男性百分比。

  select count(*) from table where state = 'nyc' and gender = 'male';

给我来自纽约的男性数量。我需要纽约男性的百分比为 75%。

+-------+-------+--------+
| name | state | gender |
+-------+-------+--------+
| Jon | nyc | male |
+-------+-------+--------+
| obama | LA | male |
+-------+-------+--------+
| kat | nyc | male |
+-------+-------+--------+
| andy | nyc | male |
+-------+-------+--------+
| sri | nyc | female |
+-------+-------+--------+

期望的输出:

州,male_percentage-->纽约,75%

纽约市共有 4 名男性,其中 3 名是男性,1 名是女性。

最佳答案

从表中选择一项)

 select state ,100 * sum( IF(gender = 'male',1,0) ) / count(*) 
from table where state = 'nyc'
group by state

关于mysql - sql查询计算百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31720452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com