dbTalk Databases Forums  

Round, Avg question.

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Round, Avg question. in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Anwei Shen
 
Posts: n/a

Default Round, Avg question. - 08-03-2010 , 03:59 PM






db2 => select avg(i), sum(i), avg(real(i)) from (values (1), (2) ) T
(i)

1 2 3
----------- ----------- ------------------------
1 3 +1.50000000000000E+000

1 record(s) selected.

I am expecting db2 to return (1+2) / 2 = 1.5, round up to 2, but not
1.

But it is returning 1.

Any special register need to set?

Thanks

Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: Round, Avg question. - 08-03-2010 , 05:32 PM






Quote:
Any special register need to set?
I think there is no such register variable, because a description for
AVG in manual "DB2 SQL Reference" was written like:
If the type of the result is integer, the fractional part of the
average is lost.

So, you may want to use a CEILING function, like this:
------------------------- Commands Entered -------------------------
SELECT avg(i) as avg_i
, sum(i) as sum_i
, CEILING( avg( real(i) ) ) as ceil_avg_i
FROM (values (1), (2) ) t(i)
;
--------------------------------------------------------------------

AVG_I SUM_I CEIL_AVG_I
----------- ----------- ------------------------
1 3 +2.00000000000000E+000

1 record(s) selected.

Reply With Quote
  #3  
Old   
ChrisC
 
Posts: n/a

Default Re: Round, Avg question. - 08-03-2010 , 05:48 PM



Or, maybe, you want to use ROUND:

SELECT avg(i) as avg_i
, sum(i) as sum_i
, ROUND( avg( real(i) ), 0 ) as ceil_avg_i
FROM (values (1), (2) ) t(i)

Reply With Quote
  #4  
Old   
Serge Rielau
 
Posts: n/a

Default Re: Round, Avg question. - 08-03-2010 , 06:41 PM



On 8/4/2010 6:48 AM, ChrisC wrote:
Quote:
Or, maybe, you want to use ROUND:

SELECT avg(i) as avg_i
, sum(i) as sum_i
, ROUND( avg( real(i) ), 0 ) as ceil_avg_i
FROM (values (1), (2) ) t(i)
Or use DECFLOAT as a datatype. In decfloat you can big from a total of 6
rounding modes (settable via DB CFG)


--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.