postgresql中的除法(/)没有给出我的答案 [英] Division ( / ) not giving my answer in postgresql

查看:1235
本文介绍了postgresql中的除法(/)没有给出我的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表software,其中的列为dev_costsell_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆