May 15, 2015 at 12:48 pm
I thought I was crazy. I have a view that returns data that looks like this (there are 500+ records):
NULL
NULL
NULL
57.875
983.875
115.75
27.35
123
NULL
NULL
NULL
NULL
NULL
And I have a query that looks like this:
select round(SUM(fldAmount3) , 0) AS X FROM myView
WHERE X = 2800
AND Y = 665
GROUP BY Y
If you don't round the total comes to 83918.5 If you round the total comes to 83918 OR 83919. Continually running the query you get the lower number every 10th time or so.
Any ideas why?
May 15, 2015 at 12:59 pm
Changes in the data between runs?
If you run a query over a static set of data, it will always return the same result (except for nondeterministic functions).
May 15, 2015 at 1:04 pm
What are the data types you are using?
May 15, 2015 at 1:04 pm
The data does not change between runs. It's static. You can run the non-rounded version a million times and it always returns the same.
Higher up the chain of views there is a calculation which generates the value we're summing on. It looks like this:
Value (float) * Percentage (int) \ 100
Again, the data is static so I would expect that however the calculation is run, the result would always be the same.
May 15, 2015 at 1:22 pm
Float is the problem. Compounded by dividing by an integer.
Is this money? Use decimal(21,5).
Floats are called inexact numerics for this reason. You may never get the same answer twice.
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
May 15, 2015 at 8:02 pm
I don't have an example at hand but I've seen the same sorts of problems with DECIMAL, as well.
As for FLOAT being inaccurate, try dividing 1 by 3 using decimal and then multiplying that answer by 3. You won't get 3. Then, try it with FLOAT.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 16, 2015 at 7:23 am
I agree, it's the many calculations that lead up to the sum. As a test I took the output of the view, 500+ records, and stored it in a temp table. You can query for the sum on this able and get consistent results every time. So that leads me to the math that produces the view.
Thanks for the insights everyone
ST
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply