我做asociated所以我有一個主表可以說與性別名稱詞典:合併2個表忽略重複
**name_dict a**
name gender
=======================
jhon male
jane female
anna female
和源數據表,其中有「重複」,我的意思是,相同的名稱,不同性別這樣的:
**name_source b**
name gender
=======================
cameron male
cameron female
anna female
travis male
我想這兩個表與此條件合併
- ignorin摹安娜(在合併條件a.name做= b.name)
- 忽略卡梅倫項(這是我卡)
我將如何創建我的合併,所以我得到這樣的結果?
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)
然後我說創造,讓測試對我們的朋友托馬斯Tschernich的sugestion,爲我所用:
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;
您可以顯示您當前的「合併」聲明,以便人們可以構建它嗎? – Mat 2013-03-13 15:56:51
我還沒有完成它,我不知道如何添加條件爲平等名稱不同性別的情況,但現在我有想法,我會在大約15分鐘後發佈它,一旦我測試它,獲得靈感! – 2013-03-13 16:23:20