我有以下代码
SELECT
Country,
COUNT(*) AS Records,
COUNT(SpecialField) AS Cnt,
COUNT(SpecialField)/COUNT(*) AS Perc_Hit
FROM
Table
GROUP BY
Country
ORDER BY
Cnt DESC, Records DESC
此查询为Perc_Hit
列返回0,即使它不应该。例如,一个中国有1000个'记录'和853'计数',所以我期待.853作为我的结果。
有人可以告诉我为什么会出现这个错误吗?
提前致谢。
答案 0 :(得分:1)
正如@ JohnCappelletti的评论所指出的,你正在进行整数除法。你可以像这样简单地将.0
添加到任何一方:
select
Country
, count(*) as Records
, count(SpecialField) as Cnt
, count(SpecialField)/(count(*)+.0) as Perc_Hit
from Table
group by Country
order by Cnt desc, Records desc
答案 1 :(得分:0)
你应该改变:
COUNT(SpecialField)/COUNT(*) AS Perc_Hit
到此:
CAST(COUNT(SpecialField) as float)/CAST(COUNT(*) as float) AS Perc_Hit