matel
matel

Reputation: 485

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

My static query so far

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

enter image description here

Upvotes: 0

Views: 81

Answers (1)

Stefanov.sm
Stefanov.sm

Reputation: 13009

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

Upvotes: 1

Related Questions