Sql 将数字结果转换为带小数位数的百分比
我有一个疑问:Sql 将数字结果转换为带小数位数的百分比,sql,postgresql,casting,aggregate-functions,percentage,Sql,Postgresql,Casting,Aggregate Functions,Percentage,我有一个疑问: Select count(incidentnumber) as average from incidents Where IncidentStationGround <> firstpumparriving_deployedfromstation; 我得到一个结果,大约是20000。但是我怎样才能把这个数字转换成一个百分比呢?另外,我想要十进制的结果,可以吗?您在注释中的查询应该可以工作 将计数强制转换为十进制以实现十进制百分比 count(incidentnumb
Select count(incidentnumber) as average
from incidents
Where IncidentStationGround <> firstpumparriving_deployedfromstation;
我得到一个结果,大约是20000。但是我怎样才能把这个数字转换成一个百分比呢?另外,我想要十进制的结果,可以吗?您在注释中的查询应该可以工作 将计数强制转换为十进制以实现十进制百分比
count(incidentnumber)::decimal*100
假设占总计数的百分比:
SELECT (count(incidentnumber) FILTER (WHERE IncidentStationGround <> firstpumparriving_deployedfromstation)
* 100.0) / count(*) AS average
FROM incidents;
将结果乘以一个数值常量:100.0加小数点!将count的bigint结果隐式转换为numeric。也可以使用显式强制转换
是可选的。您也可以返回大量的十进制数字
另外,假设incidentnumber的定义不为NULL,则count*的操作与countincidentnumber相同,速度更快。表格定义将提供这些信息
假设您运行的是当前的9.4版,那么您也忘了提供它。与旧版本的链接和备选方案相关的答案:
关于圆形和数字:
这行吗??选择countincidentnumber*100/选择countincidentnumber from incidents作为IncidentStationGround First Pumpariving_从Station部署的事件的分数;非常感谢,但是::意味着什么?::是postgres中的选角操作符吗