1

私はそのようなもののようなテーブルを持っています:

国+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とformidのポイント:異なるカタログに移動するcountry + original_form-idの場合を除いて、常に同じです。つまり、次の行にあります。

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

それから3つのテーブルを作成する必要があります。1つのテーブルは、すべての行を1:1(country + original_formからカタログ化)、2番目のN:1および3番目の1:Nの場合用です。意味:

最初のテーブル1:1

country  original_form   catalog
1        6      42
1        7      368

2番目の表1:N

country  original_form catalog
1        69      722
1        69     1837

3番目のテーブル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)
                )
        ));
4

1 に答える 1

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)
                )
        )

2番目の表:

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
        )

3番目の表:

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
        )

Table2とTable3の両方に含まれる行を見つけるには、次のコマンドを実行します。

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
        )
于 2013-02-03T12:19:38.913 に答える