[英]left outer Join missing records
I am trying to list the missing records in my PartsOnHand. 我试图在我的PartsOnHand中列出缺少的记录。 If my inventory table lists an item and it is not related to my PartsOnHand I want to list out which items are missing for each records. 如果我的库存表列出了一个项目,但它与我的PartsOnHand没有关系,我想列出每个记录中缺少的项目。
Here is the query I tried using: 这是我尝试使用的查询:
select *
from inventory i
left outer join PartsOnHand ph on i.InName = ph.InName
My table structure is as follows: 我的表结构如下:
Inventory: 库存:
InName
-----------
Wrench
Hammer
Pen
PartsOnHand: 零件:
id Inname
------------------
505 Hammer
505 Wrench
501 Wrench
501 Hammer
Desired Results: 所需结果:
505 Pen
501 Pen
Sample Data: 样本数据:
Select *
FROM (
with inventory as (
select 'Wrench' as InName from dual
union
select 'Hammer' from dual
union
select 'Pen' from dual
), partsonhand as (
select '505' as id, 'Wrench' InName from dual
union
select '505' as id, 'Hammer' InName from dual
union
select '501' as id, 'Wrench' InName from dual
union
select '501' as id, 'Hammer' InName from dual
)
select *
from inventory i
left outer join PartsOnHand ph on i.InName = ph.InName
)
Either you have an additional table ALL_ID with a column ID (primary key, and column id in PartsOnHand pointing to it), which would be the proper setup; 您可能还有一个带有列ID(主键,PartsOnHand中的列ID指向它)的附加表ALL_ID,这是正确的设置。 OR you want the ID's to be read from table PartsOnHand. 或者,您希望从表PartsOnHand中读取ID。 I will assume the former; 我假设是前者; if it's the latter, ALL_ID can be created as a subquery, by selecting distinct id from PartsOnHand 如果是后者,则可以通过从PartsOnHand中选择不同的ID将ALL_ID创建为子查询。
select id, inname
from all_id cross join inventory
where (id, inname) not in (select id, inname from partsonhand)
;
This assumes there are no NULLS in PartsOnHand (there shouldn't be any - otherwise you have data problems). 这假设在PartsOnHand中没有NULL(不应有NULL-否则会出现数据问题)。
You can do this by creating all of the different possible combinations of Id
and InName
via a CROSS JOIN
and doing a RIGHT JOIN
to that result: 您可以通过CROSS JOIN
创建Id
和InName
所有可能的不同组合,并对InName
进行RIGHT JOIN
来做到这一点:
Select L.*
From PartsOnHand P
Right Join
(
Select Id, InName
From Inventory I
Cross Join
(
Select Distinct Id
From PartsOnHand P
) X
) L On L.id = P.id
And L.InName = P.InName
Where P.id Is Null
Results: 结果:
Id InName
501 Pen
505 Pen
Seems odd to assign the same IDs to different names... (How do I know the ID of Pen is 505 and 501?) but this delivers the desired results in example: 将相同的ID分配给不同的名称似乎很奇怪...(我怎么知道Pen的ID是505和501?),但是在示例中可以达到预期的效果:
with inventory as (
select 'Wrench' as InName from dual
union
select 'Hammer' from dual
union
select 'Pen' from dual
), partsonhand as (
select '505' as id, 'Wrench' InName from dual
union
select '501' as id, 'Hammer' InName from dual
union
select '505' as id, 'Wrench' InName from dual
union
select '501' as id, 'Hammer' InName from dual
)
select C.ID, I.InName
from inventory i
LEFT join partsonhand P
on I.Inname = P.InName
cross join (Select distinct ID from partsonHand) C
Where P.ID is null
order by ID Desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.