两个表的结果作为行,连接第三个表用于其他列

时间:2018-02-15 17:08:32

标签: sql join union db2-400

尝试从3个表中提取数据:warpall, warpalq, warcmdt

我希望将warpall / warpalq中的数据作为相同结果中的行返回(首先是warpall数据,然后是warpalq数据),加入{{1}因为它有我需要返回的描述。我不能将它们联合起来,因为我需要的一个字段在warcmdt中不存在。

有人可以建议如何处理这个问题吗?

warpalq

---编辑

我现在的语法是;

select clntpm, pal#pm, clsspm, clsqpm, **commpm**  from warpall
where <criteria>

select clntpq, pal#pq, clsspq, clsqpq from warpalq
where <different criteria>

join warcmdt on warcmdt.clntcd = warpall.clntpm and warcmdt.commcd = 
warpall.commpm

1 个答案:

答案 0 :(得分:0)

知道了。对于空列,As400 / db2需要'0'。

select 
clntwf, commpm, fdsccd, clsqf, pal#wf, dtcdpm, clsswf, srb#pm, descsc
from (select substr(warpall.clntpm,1,2) as clntwf,
substr(warpall.pal#pm,1,10) as pal#wf, commpm, 
substr(warpall.clsspm,1,2) as clsswf, warpall.clsqpm * 1 as clsqwf, 
srb#pm, dtcdpm from warpall

where locnpm <> 'ASSEMBLED PALLET'
and commpm <> 'ASSEMBLED PALLET'
and clsqpm <> 0

union all

select substr(clntpq,1,2) as clntwf, substr(pal#pq,1,10) as pal#wf, '0', 
substr (clsspq,1,2) as clsswf, clsqpq * 1 as clsqwf, '0', '0' from warpalq
where clsqpq <> 0
) as t9
Inner join
warcmdt as t3
on 
t3.clntcd = t9.clntwf 
and 
t3.commcd = t9.commpm
inner join
warclss as t4
on
t4.clsssc = t9.clsswf

order by pal#wf asc