postgresql中的除法(/)没有给出我的答案 [英] Division ( / ) not giving my answer in postgresql
问题描述
我有一个表software
,其中的列为dev_cost
,sell_cost
.如果dev_cost
是16000并且sell_cost
是7500.
I have a table software
and columns in it as dev_cost
, sell_cost
. If dev_cost
is 16000 and sell_cost
is 7500.
我如何找到要出售的软件数量以恢复dev_cost
?
How do I find the quantity of software to be sold in order to recover the dev_cost
?
我已查询如下:
select dev_cost / sell_cost from software ;
它返回2作为答案.但是我们需要得到3,对吧?
It is returning 2 as the answer. But we need to get 3, right?
查询的内容是什么?预先感谢.
What would be the query for that? Thanks in advance.
推荐答案
您的列具有整数类型,并且投射至少一个要浮动的值或十进制:
Your columns have integer types, and integer division truncates the result towards zero. To get an accurate result, you'll need to cast at least one of the values to float or decimal:
select cast(dev_cost as decimal) / sell_cost from software ;
或者只是:
select dev_cost::decimal / sell_cost from software ;
然后您可以使用ceil()
函数将结果四舍五入到最接近的整数:
You can then round the result up to the nearest integer using the ceil()
function:
select ceil(dev_cost::decimal / sell_cost) from software ;
(请参见关于SQLFiddle的演示.)
这篇关于postgresql中的除法(/)没有给出我的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!