Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Split table into One to many table

I need to create 4 tables from the below 2 tables.

Province
County(FK is Provicne table id)
District(FK is County table id)
City (FK is District table id)


CREATE TABLE
    country
    (
        id BIGINT NOT NULL IDENTITY,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        County NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        District NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        City NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        ProvinceID BIGINT
    );

CREATE TABLE
    province
    (
        id INT NOT NULL IDENTITY,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        Capital NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        ProvinceCode INT,
        PRIMARY KEY (id)
    ); 

Open in new window

Avatar of chaau
chaau
Flag of Australia image

What's your tables' relationship? E.g. county is part of province, city is part of district, etc
Avatar of Zolf

ASKER

is this query correct. I am trying to compare the province name and county name as there can be duplicate county

UPDATE district
SET district.countyID = county.ID
FROM county INNER JOIN district ON county.county = district.county
INNER JOIN county on county.province = district.province;

Open in new window

Avatar of Zolf

ASKER

province is FK in county
county is FK in District
district is FK in city
city
Avatar of Zolf

ASKER

I had this excel sheet which had all of them together. now I need to use this in my application and I need to separate them in tables
Avatar of Zolf

ASKER

I created a new table District

CREATE TABLE
    District
    (
        id BIGINT NOT NULL IDENTITY,
        County NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        District NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        CountyID BIGINT
    );

Open in new window


and populated it with the id,county,province,district anem using SSIS. now I need to get the id of the county table in this district table.

I am using your reference to the previous question.but I am not sure

UPDATE district
SET district.countyID = county.ID
FROM county INNER JOIN district ON county.county = district.county
INNER JOIN county on county.province = district.province; 

Open in new window

Avatar of Zolf

ASKER

please help...I have issue that in county I have duplicate rows...so we need to first delete the duplicate rows from the county and then use that for linking other table
ASKER CERTIFIED SOLUTION
Avatar of David Todd
David Todd
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER

thanks for your help. the county table is not perfect without duplicate.but the district query of your is wrong as I don't have district name in the county table. my main table is county which has all the cols and using that to split them in individual tables
Avatar of Zolf

ASKER

basically now we have to compare the County.countyname and county.provincename with the district.countyname and district.provincename if they are same then put the countyid from the county table into the district.countyid which is fk
Avatar of Zolf

ASKER

the county table is not perfect without duplicate

my apologies. I mean now perfect without duplicate
Hi

So where are you up to - what do you need help with next?

Can you post snippets of data that illustrate your problem - helps me tune query.

Regards
  David
Avatar of Zolf

ASKER

thanks a lot, managed to get it to work with your original comments.