我在DB2 9.7.01上有树表
表A (此处为开始数据)
codeA |type
-----------------------------
P003 |P
K001 |K
表B (此处为产品数据)
codeB |description
-----------------------------
P001 |Product one
P002 |Product two
P003 |Product three
表C (此处为试剂盒数据;试剂盒包含一种或多种产品)
codeC |description |codeB
-----------------------------
K001 |Kit one |P001
K001 |Kit one |P002
我需要读取A表的每个记录,如果字段类型是“K”,则查询必须返回其多个产品;如果type为“P”,则查询只返回一个产品; 这是要完成的查询
Select a.codeA AS codeExternal,
case
when a.type = 'K' then (select C.CODEB from C where A.CODEA = C.CODEC)
else a.CODEA
end as codeInternal
from
A
left B
ON a.CODEA = B.CODEB
但查询仅返回
codeExt | code Int
---------------------------
P003 | P003
K001 | P001
而不是等待的
codeExt | code Int
---------------------------
P003 | P003
K001 | P001
K001 | P002
答案 0 :(得分:0)
尝试这样的事情:
select A.CodeA CodeExt, B.CodeB CodeInt
from A inner join B on A.CodeA=B.CodeB and A.type='P'
union all
select A.CodeA CodeExt, C.CodeB CodeInt
from A inner join C on A.CodeA=C.CodeC and A.type='K'
如果你想删除doublelon,请使用union而不是all union