简体   繁体   English

合并和源表多个匹配

[英]Merge and source table multiple matches

I face a problem with the following query: 我遇到以下查询问题:

merge into table2 d
using (
  select firstname, lastname, max(id) id
  from table1 t1
  group by firstname, lastname
  having count(0) = 1
) s
on (d.firstname=s.firstname and d.lastname=s.lastname)
when matched then update set t1_id = s.id;

If multiple rows in table2 match the ON clause, then I get "SQL Error: ORA-30926: unable to get a stable set of rows in the source tables" 如果table2中的多个行与ON子句匹配,那么我会收到“ SQL错误:ORA-30926:无法在源表中获得稳定的行集”

Do you know any way to filter and just ignore those "duplicates"? 您知道什么方法可以过滤,而忽略那些“重复项”吗? Thanks. 谢谢。

EDIT 编辑

@Polppan, your request for sample data has lead me on a very strange way: @Polppan,您对示例数据的要求以一种非常奇怪的方式引导了我:

here some sample data: 这里有一些样本数据:

table1
ID       firstname     lastname
1        John          Doe
2        John          DOE
3        Jane          Doe
4        Jane          Doe

(notice the UPPER) (请注意上方)

table2
t1_ID    firstname     lastname
null     John          Doe
null     Jane          Doe
null     Jane          Doe

now, I couldn't reproduce the error with those data until: 现在,在以下情况之前,我无法使用这些数据重现错误:

  • the ON clause is "UPPER(d.firstname)=UPPER(s.firstname) AND UPPER(d.lastname)=UPPER(s.lastname)" (which is what I have since I need case-insensitive matching) ON子句是“ UPPER(d.firstname)= UPPER(s.firstname)AND UPPER(d.lastname)= UPPER(s.lastname)”(这是我需要的,因为我需要不区分大小写的匹配)
  • one of the lines in table1 has DOE in uppercase table1中的一行之一的DOE为大写

Any idea why? 知道为什么吗?

Try using DISTINCT 尝试使用DISTINCT

MERGE INTO   table2 d
      USING  (SELECT     DISTINCT ((firstname))fname, ((lastname))lname,max(id) id
                        FROM     table1 t1
                        GROUP BY   firstname, lastname
                     HAVING  COUNT (0) = 1
                  ) s
          ON     ( upper(d.firstname) = upper(fname)
                  AND upper(lastname) = upper(lname))
WHEN MATCHED
THEN
    UPDATE SET id = s.id;

Update 1 更新1

MERGE INTO   table2 d
      USING  (SELECT     DISTINCT upper((firstname))fname, upper((lastname))lname,max(id) id
                        FROM     table1 t1
                        GROUP BY   firstname, lastname
                     HAVING  COUNT (0) = 1
                  ) s
          ON     ( upper(d.firstname) = upper(fname)
                  AND upper(lastname) = upper(lname))
WHEN MATCHED
THEN
    UPDATE SET id = s.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM