Sql 基于完全外部联接结果添加两列中的数据
来自完全外部联接的数据如下所示。我正在尝试构建一个sql查询来执行以下操作Sql 基于完全外部联接结果添加两列中的数据,sql,teradata,outer-join,Sql,Teradata,Outer Join,来自完全外部联接的数据如下所示。我正在尝试构建一个sql查询来执行以下操作 ha和da是相同的值,但任何一个都可以是无值 HC+DC(考虑HC或DC为0时,不) 我的数据是这样的 我期待这样的结果 我正在尝试的问题: select (case when h.ha not in ('None') then h.ha else h.da end) as acc, (case when h.hc not in ('None') then cast(h.hc as integer) else 0
select
(case when h.ha not in ('None') then h.ha else h.da end) as acc,
(case when h.hc not in ('None') then cast(h.hc as integer) else 0 end
+
case when h.dc not in ('None') then cast(h.dc as integer) else 0 end) as tc
from
(select h.acc as ha, hc, d.acc as da, dc from h_data h
full outer join
d_data d
on h.acc = d.acc
) h
我得到的
字符串转换为数值失败
不确定我在哪里出错。我不使用teradata虽然您的示例看起来很正确,但您的想法是正确的,您只处理null
是错误的。我猜None
只是数据库浏览器中null
的一些表示。标准coalesce
功能IMHO执行此任务:
with h_data as (
select 'AA' as acc, 1 as hc union
select 'BB' as acc, 1 as hc union
select 'EE' as acc, 1 as hc union
select 'FF' as acc, 2 as hc union
select 'GG' as acc, 1 as hc union
select 'HH' as acc, 1 as hc
), d_data as (
select 'CC' as acc, 1 as dc union
select 'DD' as acc, 1 as dc union
select 'EE' as acc, 2 as dc union
select 'GG' as acc, 4 as dc
)
select coalesce(h.acc, d.acc) as acc,
coalesce(h.hc, 0) + coalesce(d.dc, 0) as tc
from h_data h
full outer join d_data d on h.acc = d.acc
order by 1
我想你把
'None'
和NULL
混淆了。谢谢@Tomas!我的错。将null错误解释为无。我的查询在检查null时有效。我会尝试合并它看起来很简单