Zolf
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)
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)
);
What's your tables' relationship? E.g. county is part of province, city is part of district, etc
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;
ASKER
province is FK in county
county is FK in District
district is FK in city
city
county is FK in District
district is FK in city
city
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
ASKER
I created a new table District
and populated it with the id,county,province,distric t 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
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
);
and populated it with the id,county,province,distric
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
ASKER
thanks a lot, managed to get it to work with your original comments.