将SQL接口用于AS / 400(iSeries)DB2数据库,但运行并发布托盘信息可以存储在两个表中,一个用于未分配的库存(warpall)
和一个分配的股票(warpalq)
。
我需要在适用的情况下从两个表中返回记录。
warpalq
只有少数与warpall
相关的字段
我需要warpall
中的一个字段用于加入另一个表,warpalq
中没有相关字段。
我正在使用union
从一个结果集中的两个表中返回数据,然后加入其他表。
问题是,当在结果集上加入第三个表(warcmdt)
时,它在commpm
和fdsccd
字段中具有空值/ 0,因为它没有数据各自的表格。
有没有办法让我可以进入commpm
& fdsccd
回到第2行的结果集?
交联将是pal#pm = pal#pq
,因此存在相对联系。
运行下面的SQL会返回此结果集。
select
clntwf, commpm, fdsccd, clsqwf, pal#wf, dtcdpm, clsswf, srb#pm, descsc,
cor#or
from
(select substr(warpall.clntpm,1,2) as clntwf, pal#pm as pal#wf, commpm,
substr(warpall.clsspm,1,2) as clsswf, warpall.clsqpm * 1 as clsqwf, srb#pm,
dtcdpm, srd#pm
from warpall
where
locnpm <> 'ASSEMBLED PALLET'
and commpm <> 'ASSEMBLED PALLET'
and clsqpm <> 0
and clntPM <> 'D2'
and pal#pm = '1005609592'
union all
select substr(clntpq,1,2) as clntwf, pal#Pq as pal#wf, '0',
substr(clsspq,1,2) as clsswf, clsqpq * 1 as clsqwf, '0', '0', '0'
from warpalq
where
clsqpq <> 0
and clntPQ <> 'D2'
and pal#pq = '1005609592'
) as t9
left outer join warcmdt as t3
on
t3.clntcd = t9.clntwf and t3.commcd = t9.commpm
left join
warclss as t4
on
t4.clsssc = t9.clsswf
left join
warohrh as t5
on
t5.clntor = t9.clntwf
and t5.srn#or = t9.srd#pm
order by pal#wf asc
答案 0 :(得分:0)
您可以尝试使用这样的公用表表达式(CTE):
with
pal (clntwf, pal#wf, commpm, clsswf, clsqwf,
srb#pm, dtcdpm, srd#pm) as (
select
substr(warpall.clntpm,1,2) as clntwf,
warpall.pal#pm as pal#wf,
warpall.commpm,
substr(warpall.clsspm,1,2) as clsswf,
warpall.clsqpm * 1 as clsqwf,
warpall.srb#pm,
warpall.dtcdpm,
warpall.srd#pm
from warpall
where
warpall.locnpm <> 'ASSEMBLED PALLET'
and warpall.commpm <> 'ASSEMBLED PALLET'
and warpall.clsqpm <> 0
and warpall.clntPM <> 'D2'
and warpall.pal#pm = '1005609592'),
alc (clntwf, pal#wf, commpm, clsswf, clsqwf,
srb#pm, dtcdpm, srd#pm) as (
select
substr(warpalq.clntpq,1,2) as clntwf,
warpalq.pal#Pq as pal#wf,
pal.commpm,
substr(warpalq.clsspq,1,2) as clsswf,
warpalq.clsqpq * 1 as clsqwf,
'0',
'0',
pal.srd#pm
from warpalq
join pal
on waarpalq.pal#pq = pal.pal#wf
where
warpalq.clsqpq <> 0
and warpalq.clntPQ <> 'D2'
and warpalq.pal#pq = '1005609592')
select
clntwf, commpm, fdsccd, clsqwf, pal#wf, dtcdpm, clsswf, srb#pm, descsc, cor#or
from
(select *
from pal
union all
select *
from alc
) as t9
left outer join warcmdt as t3
on t3.clntcd = t9.clntwf
and t3.commcd = t9.commpm
left join warclss as t4
on t4.clsssc = t9.clsswf
left join warohrh as t5
on t5.clntor = t9.clntwf
and t5.srn#or = t9.srd#pm
order by pal#wf asc
这使您可以在多个位置重复使用pal
CTE而无需重新定义它。这意味着您可以将其加入alc
CTE以获取commpm
,以后t9
加入warcmdt
srd#pm
。现在你不应该得到那个null。我还将alc
warpall
来自t9
CTE,因为它用于warohrh
和pal.srd#pm
之间的联接。如果这不正确,您可以将'0'
替换为alc
CTE中的<p> {% allowed_contributors %} </p>
。