I’m trying to find a explode_percent
column by dividing a column to sum of the bucket.
Below is my query –
SELECT
amount,
sum(amount) over (
partition by col1,
col2,
col3,
col4
order by col2,
col3,
col4
) as explode_amount,
COALESCE(
amount / NULLIF(
SUM(amount) over(
partition by col1,
col2,
col3
order by col2,
col3,
col4
),
0
),
0
) as explode_percent
FROM my_table
However this is giving me different values in each run. I’ve tried casting both numerator and denominator to decimal(38,7)
, decimal(38,15)
, double
, round
till 7 decimal places but no luck.
Please help.
2
Answers
Thanks for the inputs.
There was some issue with the input data itself and apparently the floating point was not giving correct results. Resolved now.
Instead of using floating-point data types like
double
, consider using thedecimal
data type, which allows you to specify a fixed precision and scale also you canround
both the numerator and the denominator to a specific number of decimal places before performing the division. Rounding can help in cases where the floating-point representation introduces small errors.