[英]mySQL query giving inaccurate results
This is my current query: 这是我当前的查询:
select baseball , const.cnt/count(*) as my_percent
from full_db3 inner join
(select count(*) as cnt from full_db3 where gender = 'f') const
group by baseball
order by my_percent desc limit 10;
This yields inaccurate results. 这会导致结果不准确。 The possible values for
baseball
are TRUE and FALSE, which appear in my result's baseball
column. baseball
的可能值为TRUE和FALSE,它们显示在我的结果的baseball
栏中。 However, the values for my_percent
are all off. 但是,
my_percent
的值全部关闭。 Not even close. 差远了。
If I run this query (different GROUP BY
), I get the correct value in my_percent
for FALSE, but TRUE isn't in the result. 如果我运行此查询(不同的
GROUP BY
), my_percent
在my_percent
获得FALSE的正确值,但结果中不包含TRUE。
select baseball , const.cnt/count(*) as my_percent
from full_db3 inner join
(select count(*) as cnt from full_db3 where gender = 'f') const
group by const.cnt
order by my_percent desc limit 10;
What am I missing here? 我在这里想念什么?
How about something simpler: 简单的事情如何:
select baseball ,
(sum(case when gender = 'f' then 1 else 0 end) / count(*)) * 100 as pct
FROM full_db3
group by baseball;
This query gives percentage of females that are players / non players; 该查询给出了女性玩家(非玩家)的百分比;
select gender,
(sum(baseball) / count(baseball)) * 100 as players,
(1 - (sum(baseball) / count(baseball))) * 100 as non_players
from full_db3
where gender = 'f'
;
And the last one, that has the true / false in the rows, as finally determined as the requirement: 最后一个在行中具有true / false,最后确定为要求:
select baseball,
(count(*) /
(select count(gender) from full_db3 where gender = 'f')) * 100 as pct
from full_db3
where gender = 'f'
group by baseball;
fiddle: http://sqlfiddle.com/#!9/15866/6 小提琴: http ://sqlfiddle.com/#! 9/15866/6
It looks like you're trying to show percentages of people who play baseball. 您似乎想显示出打棒球的人的百分比。
Don't you want 你不想要
SELECT baseball, gender, (100.0 * COUNT(*) / const.cnt) AS my_percent
FROM full_db3
JOIN (SELECT COUNT(*) AS cnt FROM full_db3) CONST
GROUP BY baseball, gender
You might also want. 您可能还想要。
SELECT baseball, gender, (100.0 * COUNT(*) / const.cnt) AS my_percent
FROM full_db3
JOIN (
SELECT COUNT(*) AS cnt, gender
FROM full_db3
GROUP BY gender
) CONST ON full_db3.gender = CONST.gender
GROUP BY baseball, gender
If you want to know what percentage of 'm' and 'f' genders separately have the baseball attribute. 如果您想知道分别有'm'和'f'性别的棒球属性的百分比。
The 100.0 *
turns your fraction into a percentage 100.0 *
将您的分数变成一个百分比
Your LIMIT 10
makes no sense in this case because your aggregate query doesn't contain ten different categories. 在这种情况下,您的
LIMIT 10
毫无意义,因为您的汇总查询中没有十个不同的类别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.