0

我正在制作一个与性别相关的名字字典,所以我有一个主表可以说:

**name_dict a**
name   gender
=======================
jhon   male
jane   female
anna   female

和一个源数据表,它有“重复”,我的意思是,同名,不同性别,如下所示:

**name_source b**
name      gender
=======================
cameron   male
cameron   female
anna      female
travis    male

我想将这两个表与这个条件合并

  1. 忽略 anna(在合并条件 a.name=b.name 中完成)
  2. 忽略卡梅隆条目(这是我卡住的地方)

我将如何创建我的合并以便得到这个结果?

name      gender
----------------
jhon      male
jane      female
anna      female
travis    male

我非常感谢您的所有帮助和建议!

编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ----------------------- 所以,这是我用我的灵感创造的

merge into name_dictionary x using(
    select a.name,a.gender from name_source a, (select name,count(1) from name_source group by name having count(1)>1 order by count(1)) b
    where a.name=b.name
    ) y
    on (x.name=y.name)
    when not matched then
    insert (name,gender)
    values (y.name,y.gender)

然后我说,让我们根据我们的朋友 Thomas Tschernich 的建议对其进行测试,因为我使用了:

insert into name_dictionary
    select name,gender
    from name_source t1
    where
        (t1.name, t1.gender) not in (
            select name, gender from name_dictionary
        )
        and
        (t1.name, t1.gender) not in (
            select t2.name, t2.gender
            from name_source t2
            join name_source t3 on (t2.name = t3.name and t2.gender != t3.gender)
        );

然后彼此对抗并得到:

QUERY      EXEC TIME    FINAL ROWS  PLAN DATA
merge      2 secs        96,070         MERGE STATEMENT ALL_ROWS Cost: 253 Bytes: 46,752 Cardinality: 974 
c-Insert    killed (31m)          ¿?            INSERT STATEMENT ALL_ROWS Cost: 24,656,135 Bytes: 1,051,700 Cardinality: 105,170 

这是我使用的表的信息:

Table          Initial Rows            Observations 
name_dictionary 3,097           The ones already inserted   
name_source     101,205         The ones i  want to filter and add to the name_dictionary

(无法正确格式化,希望它可读)无论如何,我希望你能详细说明它是否正确或者我错过了什么,非常感谢!!!

---新发现如果我在合并中删除订单,成本会上升到 298;

4

1 回答 1

1

使用两个单独的插入可能比合并更容易。首先,插入表 a 中的所有条目,如下所示:

insert into name_new select * from name_dict

然后,为您的第二个表执行条件插入,如下所示:

insert into name_new
    select *
    from name_source t1
    where
        (t1.name, t1.gender) not in (
            select name, gender from name_new
        )
        and
        (t1.name, t1.gender) not in (
            select t2.name, t2.gender
            from name_source t2
            join name_source t3 on (t2.name = t3.name and t2.gender != t3.gender)
        )

第一where部分整理了 anna-case,第二部分将整理出两个性别的重复项。

于 2013-03-13T16:05:12.933 回答