2013-02-03 18 views
1

我有一個表,看起來就像這樣:SQL-如何找到不同的情況在表中

國+ form_ID + Original_form_Id +目錄

country original_form catalog form_id 
1  6     42   6 
1  7     368   7 
1  69     722   69 
1  69     1837   697 
1  659     2   659 
1  666     2   666 

original_form_id和形式ID的點:它永遠是平等的,除了 國+ original_form-ID的是去不同的目錄的情況下,在此行的意思:

country original_form catalog form_id 
1  69     722   69 
1  69     1837   697 

我需要從它創建3 TABL ES。一個表用於所有行1:1(country + original_form to catalog),第二個N:1和第三個1:N個案例。含義:

第一表1:1

country original_form catalog 
1  6  42 
1  7  368 

第二表1:N

country original_form catalog 
1  69  722 
1  69  1837 

第三表N:1

country original_form catalog 
1  659  2 
1  666  2 

我實現它使用的答案波紋管,但有重複:

INSERT INTO Mapping_1ToN 
(SELECT ot1.Country_id, ot1.original_form_id, ot1.catalog_id, ot1.Local_id 
FROM mappingtable ot1 
WHERE EXISTS -- Multiple catalogs for same country+form 
     (
     SELECT * 
     FROM mappingtable ot2 
     WHERE ot1.country_id = ot2.country_id 
       AND ot1.original_form_id = ot2.original_form_id 
       AND ot1.form_id <> ot2.form_id 
       AND ot1.catalog_id <> ot2.catalog_id 
     )); 

INSERT INTO Mapping_NTo1 
(SELECT ot1.Country_id, ot1.original_form_id, ot1.catalog_id, ot1.Local_id 
FROM mappingtable ot1 
WHERE EXISTS -- Multiple forms for same catalog 
     (
     SELECT * 
     FROM mappingtable ot2 
     WHERE ot1.country_id = ot2.country_id 
       AND ot1.original_form_id <> ot2.original_form_id 
       AND ot1.catalog_id = ot2.catalog_id 
     )); 


INSERT INTO Mapping_1To1 
(SELECT ot1.Country_id, ot1.original_form_id, ot1.catalog_id, ot1.Local_id 
FROM mappingtable ot1 
WHERE NOT EXISTS -- form+catalog unique per country 
     (
     SELECT ot2.Country_id, ot2.original_form_id, ot2.catalog_id, ot2.Local_id 
     FROM mappingtable ot2 
     WHERE ot1.country_id = ot2.country_id 
       AND (
        (ot1.original_form_id = ot2.original_form_id AND ot1.catalog_id <> ot2.catalog_id AND ot1.form_id = ot2.form_id) 
        OR 
        (ot1.original_form_id <> ot2.original_form_id AND ot1.catalog_id = ot2.catalog_id) 
       ) 
     )); 
+2

請不要猶豫,以答覆答案並將其標記爲已接受。 –

+0

但我沒有得到一個可行的答案... –

回答

1

第一個表:

insert Table1 
select * 
from OriginalTable ot1 
where   not exists -- form+catalog unique per country 
        (
        select  * 
        from    OriginalTable ot2 
        where   ot1.country = ot2.country 
       and (
        (ot1.form = ot2.form and ot1.catalog <> ot2.catalog) 
        or 
        (ot1.form <> ot2.form and ot1.catalog = ot2.catalog) 
       ) 
       ) 

二表:

insert Table2 
select ot1.* 
from OriginalTable ot1 
where exists -- Multiple catalogs for same country+form 
     (
     select * 
     from OriginalTable ot2 
     where ot1.country = ot2.country 
       and ot1.form = ot2.form 
       and ot1.catalog <> ot2.catalog 
     ) 

三表:

insert Table3 
select ot1.* 
from OriginalTable ot1 
where exists -- Multiple forms for same country+catalog 
     (
     select * 
     from OriginalTable ot2 
     where ot1.country = ot2.country 
       and ot1.form <> ot2.form 
       and ot1.catalog = ot2.catalog 
     ) 

要找到將在表2中結束的行和表3運行:

select * 
from OriginalTable ot1 
where   exists 
        (
        select  * 
        from    OriginalTable ot2 
     where ot1.country = ot2.country 
       and ot1.form <> ot2.form 
       and ot1.catalog = ot2.catalog 
       ) 
     and exists 
        (
        select  * 
        from    OriginalTable ot2 
     where ot1.country = ot2.country 
       and ot1.form = ot2.form 
       and ot1.catalog <> ot2.catalog 
       ) 
+0

1:1情況如何?你爲什麼只選擇一切? –

+0

啊哈,我以爲'1:1'就是一個普通的副本。已更新,因此它只包含每個國家/地區具有一個「(表單,目錄)」組合的行。 – Andomar

+0

@Andomar在你的第一個表..你的意思是這個'選擇OT1。* 從OriginalTable ot1' – exexzian

相關問題