James Foreman wrote:
When I do a describe on your statement, I see that the resulting data type
of the division is DECIMAL(31, 0). So there are no digits after the
decimal point. Given that, your example above acts as one would expect.
$ db2 "describe SELECT sum(cost_of_sale) / sum(price) AS division2 FROM
testsums"
sqltype sqllen
-------------------- ------
485 DECIMAL 31, 0
I can't reproduce the result "0.5" that you got with 1000 and 2000. What
exactly did you do there?
To address this, you might want to adjust your data types like this (or
something similar):
$ db2 "SELECT decimal(sum(cost_of_sale), 7, 2) / decimal(sum(price), 9, 2)
FROM testsums"
1
---------------------------------
0.333333333333333333333333
1 record(s) selected.
Thanks Knut. That does fix the rounding error, but it does still
confuse me why the resulting data type is decimal (31, 0) - I'd have
expected DB2 to preserve the level of accuracy from the base data
type. (It will quickly become tiresome to do this every time we have
to carry out such a division!)
There are factors influencing this:
1) When you do a SUM on a decimal(p,s) column, your result will
always be DECIMAL(31,s).
This happens in order to avoid overflows in the datatype, and
also explains why Knut's suggestion of casting the sum as
decimal(7,2) happens to work with your example data.
2) The reason that you get a result of 0. is a function of how DB2
does decimal division.
From the manual (Messages Reference, SQL0419N):
The formula used internally to calculate the scale of the result
for decimal division is:
Scale of result = 31 - np + ns - ds
where np is the precision of the numerator, ns is the scale of
the numerator, and ds is the scale of the denominator.
Dividing 2 decimal(31,2) results yields a result that's decimal(31,0)
(as you experienced).
To solve this: The better solution is to cast your SUM() results into
the correct precision. Of course, this could be somewhat time consuming,
as you've found.
There is also another solution:
The database configuration parameter MIN_DEC_DIV_3 can be set to YES,
which ensures that all decimal division operations will have a minimum
scale of 3. This parameter is not shown in the output of 'get db cfg'.
If you set this, you'll get a result (from your original query) of
0.500.
There are some potential complications with doing this, so check the
manual for the caveats if you go this route.
Good luck,
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----