Sum with Negative and Positive Numbers - Sql Server

Asked

Viewed 958 times

0

I am trying to sum up positive and negative values in the Sqlserver database, but the same done by the calculator returns a different value.

I’m using

SUM(NR_QUANTIDADE) 

The NR_QUANTIDADE column is of the float type in the database.

My values in the database are:

-9,9961
-30,41
-19,5939
60

Already the result of my select is returning:

-5,32907051820075E-15

And it would be right to return 0.

  • 1

    Enter your select here

  • Its problem is not of positive or negative numbers but of precision pq is impossible for the computer to have infinite precision and so there are always inaccuracies when dealing with decimal numbers. There are many questions about this here on the site, you can delete this issue that is redundant is looking for posts like these:https://answall.com/search?q=%5Bsql%5D+error+rounding

1 answer

0


I put together an example based on what you posted:

create table temp (valor float)

insert into temp (valor) values (-9.9961)
insert into temp (valor) values (-30.41)
insert into temp (valor) values (-19.5939)
insert into temp (valor) values (60)

select sum(valor) total from temp

SQL Fiddle: http://www.sqlfiddle.com/#! 18/2cd33d/1/0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.