union all 后只返回一条记录
Only one record returned after union all
使用 SQL 接口进入 AS/400 (iSeries) DB2 数据库,但 运行 进入并发布托盘信息 可以 存储的位置在两个 table 中,一个用于非分配股票 (warpall)
,一个用于分配股票 (warpalq)
。
我需要 return 来自两个 table 的记录(如果适用)。
warpalq
只有少数与 warpall
相关的字段
warpall
中我需要的字段之一也被用来加入另一个table,warpalq
中没有相关字段。
我正在使用 union
来 return 来自一个结果集中两个 table 的数据,然后加入另一个 tables。
问题是,在结果集上加入第三个 table (warcmdt)
时,它在 commpm
和 fdsccd
字段中有空值 / 0在相应的 table.
中没有相关数据
有什么方法可以将 commpm
和 fdsccd
拉回到第 2 行的结果集中?
交叉 link 将是 pal#pm = pal#pq
所以有一个相对 link.
运行 此结果集 return 下面的 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
您可以像这样尝试使用通用 Table 表达式 (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
从 warpall
带入 alc
CTE,因为它用于 t9
和 warohrh
之间的连接。如果这不正确,您可以在 alc
CTE 中将 pal.srd#pm
替换为 '0'
。
使用 SQL 接口进入 AS/400 (iSeries) DB2 数据库,但 运行 进入并发布托盘信息 可以 存储的位置在两个 table 中,一个用于非分配股票 (warpall)
,一个用于分配股票 (warpalq)
。
我需要 return 来自两个 table 的记录(如果适用)。
warpalq
只有少数与 warpall
相关的字段
warpall
中我需要的字段之一也被用来加入另一个table,warpalq
中没有相关字段。
我正在使用 union
来 return 来自一个结果集中两个 table 的数据,然后加入另一个 tables。
问题是,在结果集上加入第三个 table (warcmdt)
时,它在 commpm
和 fdsccd
字段中有空值 / 0在相应的 table.
有什么方法可以将 commpm
和 fdsccd
拉回到第 2 行的结果集中?
交叉 link 将是 pal#pm = pal#pq
所以有一个相对 link.
运行 此结果集 return 下面的 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
您可以像这样尝试使用通用 Table 表达式 (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
从 warpall
带入 alc
CTE,因为它用于 t9
和 warohrh
之间的连接。如果这不正确,您可以在 alc
CTE 中将 pal.srd#pm
替换为 '0'
。