简体   繁体   中英

How to set precision on calculated field

I joined four tables so I could calculate the total price of a sales transaction. The code that I used to join all four tables is also listed below. My issue is that it is calculating a result that has a precision of four decimal places but I would like it to only have a precious of two.

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;

So, the result is: 在此处输入图片说明

How can I make the result have a precision of only two decimal places instead of four? Thank you

Use round( column_name , 2) as follows -

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM