Oracle performs operation in the order you specified.
So, if you write this query:
select SUM(numWithScale/7) from some_table -- (1)
each of values divided by 7 and rounded to maximum available precision: NUMBER
with 38 significant digits. After that all digits are summed.
In case of this query:
select SUM(numWithScale)/7 from some_table -- (2)
all numWithScale
values are summed and only after that divided by 7. In this case there are no precision loss for each record, only result of sum()
division by 7 are rounded to 38 significant digits.
This problem are common for calculation algorithms. Each time when you divide value by 7 you produce small calculation error because of limited number of digits, representing a number:
numWithScale/7 => quotient + delta
.
While summing this values you got
sum(quotient) + sum(delta)
.
If numWithScale
represents ideal uniform distribution and and a some_table
contains infinite number of records, then sum(delta)
tends to zero. But it happens only in theory. In practical cases sum(delta)
grows and introduces significant error. This is a case of query(1)
.
On the other hand, summing can't introduce a rounding error if implemented properly. So for query (2) rounding error introduced only in last step, when whole sum divided by 7. Therefore value of delta
for this query not affected by number of records.