我明白了:
错误2616(计算中发生数字溢出)
当我运行下面的代码时。我开发了两个单独的查询,每个查询运行当我将它们放在一个查询中并使用UNION
时,我收到错误消息。联合的一侧返回274条记录,另一条返回277条记录。当我收到此消息时,我正在使用Teradata SQL Assistant
。我们使用的是版本14.10.0.07
。
with drvd_qry (operating_unit, grp_brn_id, ecr_dept_id, stn_id, glt_seq) as
(select
soh.operating_unit,
s.grp_brn_id,
s.ecr_dept_id,
s.stn_id,
s.glt_seq
from stns s
inner join rfs.stn_ops_hierarchies soh on soh.stn_stn_id = s.stn_id
where substr(s.grp_brn_id, 1, 2) = 'G1'
group by soh.operating_unit, s.grp_brn_id, s.ecr_dept_id, s.stn_id, s.glt_seq),
qry_drvd (ecr_ticket_no, open_item_id) as
(select
j.ecr_ticket_no,
j.open_item_id
from
rfs.journal_entries j
where
j.business_unit ='A0141'
and j.accounting_date = cast ('23-SEP-2015' as date format 'dd-MMM-YYYY')
and j.account_gl ='109850')
select
dq.operating_unit as BU,
dq.grp_brn_id as GPBR,
dq.stn_id as STN_ID,
j.department as DEPTID,
ft.mrchnt_nbr as MERCH_NUM,
j.ecr_ticket_no as TICKET_NUM,
ft.prim_acct_frst_six_dgt_nbr as FIRST6,
ft.prim_acct_last_four_dgt_nbr as LAST4,
p.auth_nbr as AUTH_NUM,
ft.stlmt_uniq_ref_nbr as REF_NUM,
j.monetary_amount as GL_AMT,
0.00 as BANK_AMT
from
rfs.journal_entries j,
rfs.pymts p,
paymt.fin_tran ft,
drvd_qry dq
where
j.business_unit = 'A0141'
and j.accounting_date = cast ('23-SEP-2015' as date format 'dd-MMM-YYYY')
and j.account_gl in (109850)
and cast(j.open_item_id as decimal(19,0)) = p.ecr_pymt_id
and p.ram_rea_rnt_agr_nbr = j.rnt_agr_nbr
and p.fin_tran_ref_id = ft.fin_tran_ref_id
and dq.ecr_dept_id = j.department
UNION
select
b.BU,
b.GPBR,
b.STN_ID,
b.DEPTID,
b.MERCH_NUM,
qd.ecr_ticket_no as TICKET_NUM,
b.FIRST6,
b.LAST4,
b.AUTH_NUM,
b.REF_NUM,
b.GL_AMT,
b.BANK_AMT
from
(select
a.BU,
a.GPBR,
a.STN_ID,
a.DEPTID,
a.MERCH_NUM,
a.REF_NUM,
a.FIRST6,
a.LAST4,
p.auth_nbr as AUTH_NUM,
p.ecr_pymt_id,
a.GL_AMT,
a.BANK_AMT
from
(select
dq.operating_unit as BU,
dq.grp_brn_id as GPBR,
dq.stn_id as STN_ID,
dq.ecr_dept_id as DEPTID,
cast(f.merch_num as varchar(20)) as MERCH_NUM,
f.ret_ref_num as REF_NUM,
ft.prim_acct_frst_six_dgt_nbr as FIRST6,
ft.prim_acct_last_four_dgt_nbr as LAST4,
0.00 as GL_AMT,
case when f.tran_typ_cde = 1 then f.tran_amt
when f.tran_typ_cde = 4 then f.tran_amt * -1
end as BANK_AMT,
ft.fin_tran_ref_id
from paymt.fndng_recncl_dtl_rprt f,
rfs.cc_mrchnt_nbr m,
drvd_qry dq,
paymt.fin_tran ft
where f.row_stat_cde = 'A'
and cast (f.tran_proc_date as date format 'MM/DD/YYYY') ='09/23/2015'
and m.mrchnt_nbr = f.merch_num
and m.credit_card_typ = 'VI'
and dq.stn_id = m.sta_stn_id
and ft.stlmt_uniq_ref_nbr = f.ret_ref_num
group by
dq.operating_unit,
dq.grp_brn_id,
dq.stn_id,
dq.glt_seq,
dq.ecr_dept_id,
f.merch_num,
f.ret_ref_num,
ft.prim_acct_frst_six_dgt_nbr,
ft.prim_acct_last_four_dgt_nbr,
GL_AMT,
BANK_AMT,
ft.fin_tran_ref_id) a
left outer join rfs.pymts p on p.fin_tran_ref_id = a.fin_tran_ref_id) b
left outer join qry_drvd qd on cast(qd.open_item_id as decimal(19,0)) = b.ecr_pymt_id
答案 0 :(得分:2)
我假设你的查询单独运行都没问题。 UNION
运算符:
将两个或多个查询的结果合并到一个结果集中 包括属于union中所有查询的所有行。 UNION操作与使用组合列的连接不同 来自两张桌子。
以下是组合两个结果集的基本规则 使用UNION进行查询:
所有查询中列的数量和顺序必须相同。
数据类型必须兼容。
我无法看到您的数据并重现它,但我猜您的DECIMAL/NUMERIC
列与第二个SELECT
不兼容。检查它的最简单方法是对两个语句中的所有列进行注释,除了一个,并运行查询,每次都取消注释一列。当您找到使用哪个列导致错误时:
CAST(col_name AS type) -- where type is broader type
修改强>
来自 @dnoeth
评论:
可能你需要投射硬编码0.00 AS BANK_AMT
,其被视为DECIMAL(3,2)
:
CAST(0.00 AS DECIMAL(18,2)) AS BANK_AMT
来自 @anwaar_hell
评论和 Teradata UNION
:
所有与UNION结合的SQL语句都必须返回相同的内容 此外,列的数量是所有列的数据类型 参与的选择语句必须匹配。 如果他们不这样做 匹配,第一个SQL select语句的数据类型将是 另一个选择的相关列和不匹配列 语句将被隐式地转换为相同的数据类型 第一个选择语句有。
请记住这一点,特别是如果您的列是字符数据类型, 因为这可能导致文本列的隐藏截断;这是一个 这个问题很难发现。
在其他RDBMS(如Oracle / SQL Server)中,数据类型被隐式转换为更广泛的数据类型。
答案 1 :(得分:0)
纠正这个
CAST(0 AS DECIMAL(18,2)) for BANK_AMT