Teradata SQL表连接不同格式的键

时间:2018-04-13 22:09:07

标签: sql join casting overflow teradata

我有一个数据格式问题,我使用从两个不同的表加入的帐户级标识符(accno),它最初给了我FLOAT类型错误的精度错误。

select rcv.Product, rcv.accno, rcv.date,
            bfv.Prod_class_cd, bfv.acc_appsys_id, acc_co_no
from FINAL_RECOVERY rcv
inner join RTL_EXPSR_MTRL bfv
    on rcv.accno = bfv.accno and rcv.date = bfv.date
where rcv.Product is null
order by rcv.date;

所以我尝试将accno转换成其他各种格式,但仍然没有运气。

on cast(rcv.accno as int) = cast(bfv.accno as int) and rcv.date = bfv.date

on cast(rcv.accno as decimal(12,0)) = cast(bfv.accno as decimal(12,0)) and rcv.date = bfv.date

on cast(rcv.accno as char) = cast(bfv.accno as char) and rcv.date = bfv.date

将它们转换为int或decimals会给出溢出错误消息,如果我转换为字符,则会耗尽假脱机空间。铸造我唯一的选择吗?您对如何解决这个问题的建议是什么?

1 个答案:

答案 0 :(得分:2)

当你得到浮点精度错误时,你似乎有一个数字列和一个超过15位的VarChar。

-- INTEGERs only got up to 10 digits = less than 15 = numeric overflow
cast(rcv.accno as int) = cast(bfv.accno as int)
-- decimal(12,0) got 12 digits = less than 15 = numeric overflow
cast(rcv.accno as decimal(12,0)) = cast(bfv.accno as decimal(12,0))
-- cast as char = cast as CHAR(1) = Teradata silently truncating to a single digit
-- = join is similar to a Cross Join = no more spool space
cast(rcv.accno as char) = cast(bfv.accno as char)

保持数字列不变,并将另一列转换为相同的数据类型。这将起作用,除非你得到错误的数据,然后使用TO_NUMBER(accno)或TD15.10' TRYCAST,两者都将为坏数据返回NULL,从而排除该行。