如何在计算字段上设置精度

时间:2018-05-02 21:14:24

标签: sql precision

我加入了四张桌子,因此我可以计算销售交易的总价。我用来连接所有四个表的代码也列在下面。我的问题是,它正在计算一个精度为四位小数的结果,但我希望它只有两个宝贵的。

select ol.order_number, co.customer_id, ol.product_id, ol.product_quantity, ol.product_price, sum(ol.product_quantity * ol.product_price) as "Net Total",
zc.sales_tax_applied as "Sales Tax", (sum(ol.product_quantity * ol.product_price) + (sum(ol.product_quantity * ol.product_price) * zc.sales_tax_applied)) as "Total Price"
from order_line ol inner join customer_order co
on ol.order_number = co.order_number
inner join customer c
on co.customer_id = c.customer_id
inner join zipcode zc
on c.zipcode = zc.zipcode
group by ol.order_number, co.customer_id, ol.product_id, ol.product_quantity, ol.product_price, zc.sales_tax_applied;

所以,结果是: enter image description here

如何使结果的精度只有两位小数而不是四位?谢谢

1 个答案:

答案 0 :(得分:1)

使用 round( column_name ,2),如下所示 -

select ol.order_number, co.customer_id, ol.product_id, ol.product_quantity, round(ol.product_price,2) as product_price, round(sum(ol.product_quantity * ol.product_price),2) as "Net Total",
round(zc.sales_tax_applied,2) as "Sales Tax", round((sum(ol.product_quantity * ol.product_price) + (sum(ol.product_quantity * ol.product_price) * zc.sales_tax_applied)),2) as "Total Price"
from order_line ol inner join customer_order co
on ol.order_number = co.order_number
inner join customer c
on co.customer_id = c.customer_id
inner join zipcode zc
on c.zipcode = zc.zipcode
group by ol.order_number, co.customer_id, ol.product_id, ol.product_quantity, ol.product_price, zc.sales_tax_applied;