问题:Teradata中四舍五入规则和标准规则有差异?如下例:
select cast('1212.705' as decimal(18,2))
--1212.70
select cast('1212.715' as decimal(18,2))
--1212.72
解答:Teradata中四舍五入的规则由DBSCONTROL下的参数 RoundHalfwayMagUp 决定,默认 RoundHalfwayMagUp = FALSE,此时按Teradata的规则进行进位,Teradata的规则考虑到5是个中间值,都进位则概率不平均,如果取决于前一位的奇偶,则进位的 5和舍掉的5是平均的,根据这个原则,Teradata的四舍五入规则为:当5后面有1位不为0时,则进位,当5后面位都为0或者没有其他位时根据5前面 一位的奇偶,奇进偶不进,下面是一些例子:
select cast('1212.705' as decimal(18,2))
--1212.70
select cast('1212.715' as decimal(18,2))
--1212.72
select cast('1212.7051' as decimal(18,2))
--1212.71
select cast('1212.7052' as decimal(18,2))
--1212.71
select cast('1212.70500' as decimal(18,2))
--1212.70
select cast('1212.705001' as decimal(18,2))
--1212.71
设置RoundHalfwayMagUp = true,(设置成功需要重启数据库才能生效)则按标准的四舍五入进行取舍,如下例:
select cast('1212.705' as decimal(18,2))
--1212.71
select cast('1212.715' as decimal(18,2))
--1212.72
select cast('1212.7051' as decimal(18,2))
--1212.71
select cast('1212.7052' as decimal(18,2))
--1212.71
select cast('1212.70500' as decimal(18,2))
--1212.71
select cast('1212.705001' as decimal(18,2))
--1212.71
解決方案
/Adding a 0.000000001 to make teradata do the RoundHalfwayMagUp/ CAST(COLX + 0.000000001 AS DECIMAL (6,2)) AS COLY