0

我遇到以下查询的问题:

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;

如果 table2 中的多行与 ON 子句匹配,则会收到“SQL 错误:ORA-30926:无法在源表中获得一组稳定的行”

你知道有什么方法可以过滤并忽略那些“重复”吗?谢谢。

编辑

@Polppan,您对样本数据的要求使我以一种非常奇怪的方式引导我:

这里有一些示例数据:

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

(注意上)

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

现在,我无法用这些数据重现错误,直到:

  • ON 子句是“UPPER(d.firstname)=UPPER(s.firstname) AND UPPER(d.lastname)=UPPER(s.lastname)”(这是我所拥有的,因为我需要不区分大小写的匹配)
  • table1 中的其中一行的 DOE 为大写

知道为什么吗?

4

1 回答 1

1

尝试使用 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;

更新 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;
于 2013-09-23T13:18:43.477 回答