Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:35:04+00:00 2026-06-18T08:35:04+00:00

I have a table that looks something like that: country + form_ID + Original_form_Id

  • 0

I have a table that looks something like that:

country + form_ID + Original_form_Id + catalog

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

The point of original_form_id and form id: it is always equal, besides the case of
country +original_form-id that go to different catalogs, meaning in this rows:

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

I need to create from it 3 tables. One table is for all rows 1:1 (country+original_form to catalog), Second N:1 and Third 1:N cases. Meaning:

First table 1:1

country  original_form   catalog
1        6      42
1        7      368

Second table 1:N

country  original_form catalog
1        69      722
1        69     1837

Third table N:1

country  original_form   catalog
1        659      2
1        666     2

I implement it using the answer bellow but there are duplicates:

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)
                )
        ));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:35:05+00:00Added an answer on June 18, 2026 at 8:35 am

    First table:

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

    Second table:

    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
            )
    

    Third table:

    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
            )
    

    To find rows that would end up in both Table2 and Table3, run:

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks something like this: id | firstperson | secondperson
I have a table that looks something like this: Name Year Value A 2000
I have a MySQL table that looks something like this: +-----+------------+ | id |
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a multi-row insert that looks something like: insert into table VALUES (1,
I have a table that looks something like this: +----------------------+-------------------+ | This text is
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have a table that looks something like this |ID|TIME-| |01|01:15| |01|01:30| |02|01:35| |02|01:36|
So lets say I have a table that looks something like this: ItemName ProductType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.