Home > Software design >  PostgreSQL want to round percentile_cont
PostgreSQL want to round percentile_cont

Time:01-30

I am computing a range of values in PostgreSQL, and I want to round some of them to 2 decimal places. when I do round the average it works round(avg(rate), 2) as avg_rate. when I round the median round(percentile_cont(0.5) within group (order by rate) as med, it gives an error saying I need to add explicit type casts. the percentile_cont works fine without the round, but I am unsure how to fix this. any help would be appreciated. thanks

CodePudding user response:

percentile_count() in this case returns double precision. You need to cast it to numeric, as there is no round(double precision) version of the function.

round(percentile_cont(0.5) within group (order by rate)::numeric, 2) as med

CodePudding user response:

Type casting it to decimal will also round the median

percentile_cont(0.5) within group (order by rate)::decimal(8,2) as median
  •  Tags:  
  • Related