Home > Software engineering >  postgres round by variable decimal place
postgres round by variable decimal place

Time:12-22

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

My static query so far

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

enter image description here

CodePudding user response:

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

  • Related