sql server - TSQL - Do you calculate values then sum, or sum first then calculate values? -


i feel stupid asking - there math rule forgetting.

i trying calculate gross profit based on net sales, cost, , billbacks.

i 2 different values based on how calculation:

(sum(netsales) - sum(cost)) + sum(billbackdollars) calculateoutsidesum, sum((netsales - cost) + billbackdollars) calculatewithinsum 

this coming off of basic transaction fact table.

in particular example, there 90 records being summed, , following results

calculateoutsidesum: 234.77 calculatewithinsum:  247.70 

i imagined sort of transitive property , both results same considering it's summation.

which method correct?

from mathematical point of view, should same value both formulas.

anyway in cases it's better performs sum after calculation.

edit after opener response:

and treat data isnull function or other casting function increases data precision.

rounding, formatting , castings decreases data precision should applied after sums.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -