从 LEFT OUTER JOIN 中删除重复项

发布于 2024-08-27 01:23:52 字数 948 浏览 8 评论 0原文

我的问题与 限制 LEFT JOIN 非常相似,但有一些变化。

假设我有一个表 SHOP 和另一个表 LOCATION。 Location 是 SHOP 表的一种子表,它有两列感兴趣的列,一列是分区键(简称 KEY)和“SHOP”编号。这与表 SHOP 中的数字“NO”匹配。

我尝试了这个左外连接:

SELECT S.NO, L.KEY
FROM SHOP S
LEFT OUTER JOIN LOCATN L ON S.NO = L.SHOP

但我收到了很多重复项,因为有很多位置属于单个商店。我想消除它们并只获得一个不重复的“商店,钥匙”条目列表。

数据正确,但重复项显示如下:

SHOP     KEY
 1       XXX
 1       XXX
 2       YYY
 3       ZZZ
 3       ZZZ  etc.

我希望数据显示如下:

SHOP     KEY
 1       XXX
 2       YYY
 3       ZZZ  etc.

SHOP 表:

 NO
 1       
 2       
 3       

LOCATION 表:

 LOCATION   SHOP  KEY
   L-1       1    XXX   
   L-2       1    XXX   
   L-3       2    YYY   
   L-4       3    YYY   
   L-5       3    YYY   

(ORACLE 10g 数据库)

My question is quite similar to Restricting a LEFT JOIN, with a variation.

Assuming I have a table SHOP and another table LOCATION. Location is a sort of child table of table SHOP, that has two columns of interest, one is a Division Key (calling it just KEY) and a "SHOP" number. This matches to the Number "NO" in table SHOP.

I tried this left outer join:

SELECT S.NO, L.KEY
FROM SHOP S
LEFT OUTER JOIN LOCATN L ON S.NO = L.SHOP

but I'm getting a lot of duplicates since there are many locations that belong to a single shop. I want to eliminate them and just get a list of "shop, key" entries without duplicates.

The data is correct but duplicates appear as follows:

SHOP     KEY
 1       XXX
 1       XXX
 2       YYY
 3       ZZZ
 3       ZZZ  etc.

I would like the data to appear like this instead:

SHOP     KEY
 1       XXX
 2       YYY
 3       ZZZ  etc.

SHOP table:

 NO
 1       
 2       
 3       

LOCATION table:

 LOCATION   SHOP  KEY
   L-1       1    XXX   
   L-2       1    XXX   
   L-3       2    YYY   
   L-4       3    YYY   
   L-5       3    YYY   

(ORACLE 10g Database)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺渁∝ 2024-09-03 01:23:52

您需要按“S.No”和“GROUP BY”进行分组'L.KEY'

SELECT S.NO, L.KEY 
FROM SHOP S 
LEFT OUTER JOIN LOCATN L 
ON S.NO = L.SHOP
GROUP BY S.NO, L.KEY

You need to GROUP BY 'S.No' & 'L.KEY'

SELECT S.NO, L.KEY 
FROM SHOP S 
LEFT OUTER JOIN LOCATN L 
ON S.NO = L.SHOP
GROUP BY S.NO, L.KEY
青柠芒果 2024-09-03 01:23:52

编辑 在您的场景中进行更新后,

我认为您应该能够使用简单的子查询来完成此操作(尽管我还没有针对 Oracle 数据库对此进行测试)。类似下面的内容

UPDATE shop s
SET divnkey = (SELECT DISTINCT L.KEY FROM LOCATN L WHERE S.NO = L.SHOP)

如果商店与多个部门的位置关联,则以上内容将引发错误。

如果您只是想忽略这种可能性并在这种情况下选择任意一个,您可以使用

UPDATE shop s
SET divnkey = (SELECT MAX(L.KEY) FROM LOCATN L WHERE S.NO = L.SHOP)

EDIT Following the update in your scenario

I think you should be able to do this with a simple sub query (though I haven't tested this against an Oracle database). Something like the following

UPDATE shop s
SET divnkey = (SELECT DISTINCT L.KEY FROM LOCATN L WHERE S.NO = L.SHOP)

The above will raise an error in the event of a shop being associated with locations that are in multiple divisions.

If you just want to ignore this possibility and select an arbitrary one in that event you could use

UPDATE shop s
SET divnkey = (SELECT MAX(L.KEY) FROM LOCATN L WHERE S.NO = L.SHOP)
你又不是我 2024-09-03 01:23:52

我也遇到了这个问题,但我无法使用 GROUP BY 来修复它,因为我还返回了 TEXT 类型字段。 (使用 DISTINCT 也是如此)。

这段代码给了我重复项:

select mx.*, case isnull(ty.ty_id,0) when 0 then 'N' else 'Y' end as inuse 
from master_x mx 
left outer join thing_y ty on mx.rpt_id = ty.rpt_id

我通过这样重写来修复它:

select mx.*, 
case when exists (select 1 from thing_y ty where mx.rpt_id = ty.rpt_id) then 'Y' else 'N' end as inuse
from master_x mx 

如你所见,我不关心第二个表(thing_y)中的数据,只关心是否有大于零的匹配其中的 rpt_id 。 (仅供参考:rpt_id 也不是第一个表 master_x 上的主键)。

I had this problem too but I couldn't use GROUP BY to fix it because I was also returning TEXT type fields. (Same goes for using DISTINCT).

This code gave me duplicates:

select mx.*, case isnull(ty.ty_id,0) when 0 then 'N' else 'Y' end as inuse 
from master_x mx 
left outer join thing_y ty on mx.rpt_id = ty.rpt_id

I fixed it by rewriting it thusly:

select mx.*, 
case when exists (select 1 from thing_y ty where mx.rpt_id = ty.rpt_id) then 'Y' else 'N' end as inuse
from master_x mx 

As you can see I didn't care about the data within the 2nd table (thing_y), just whether there was greater than zero matches on the rpt_id within it. (FYI: rpt_id was also not the primary key on the 1st table, master_x).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文