根据全外连接结果从两列添加数据

Add data from two columns based on full outer join result

我的 Full outer join 数据如下所示。我正在尝试构建一个 sql 查询来执行以下操作。

  1. ha 和 da 是相同的值,但任何一个都可以 None
  2. 做hc+dc(当hc或dc为None时认为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 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

我得到 A character string failed conversion to a numeric value 不确定我在哪里犯了错误。

我不使用 teradata,虽然你的例子在我看来你的想法是正确的,你只是把 null 看错了。我想 None 只是 null 在您的数据库浏览器中的一些表示。标准 coalesce 函数恕我直言完成工作:

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

Tested on PG.