Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

postgres round by variable decimal place

I have a rounding length value by contract. I would like to round my data table by this value however I cannot find how to input it dynamically (ie changing 2 in the example below by 5). Is it possible?

Table trading.data18 structure
idcontract integer
close double

Table contracts structure
idcontract integer
rounding double

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My static query so far

select ROUND(CAST(close AS numeric),2) from trading.data18 limit 10;

enter image description here

>Solution :

You can use contracts.rounding as a second argument of round.

select round(d.close, c.rounding::integer)::numeric
from trading.data18 as d
join contracts as c using(idcontract);

using(idcontract) is a shorthand for on c.idcontract = d.idcontract

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading