Solved

Split table into One to many table

Posted on 2013-06-19
12
288 Views
Last Modified: 2016-02-11
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

0
Comment
Question by:zolf
  • 9
  • 2
12 Comments
 
LVL 24

Expert Comment

by:chaau
Comment Utility
What's your tables' relationship? E.g. county is part of province, city is part of district, etc
0
 

Author Comment

by:zolf
Comment Utility
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

0
 

Author Comment

by:zolf
Comment Utility
province is FK in county
county is FK in District
district is FK in city
city
0
 

Author Comment

by:zolf
Comment Utility
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
0
 

Author Comment

by:zolf
Comment Utility
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

0
 

Author Comment

by:zolf
Comment Utility
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
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
Comment Utility
Hi,

From the original county table - rename county to county_old - try this

insert dbo.county( county, provinceID )
select distinct county, provinceID
from dbo.county_old

insert dbo.district( district, countyid )
select distinct co.district, cn.countyid
from dbo.county_old co
inner join dbo.county cn
    on cn.county = co.county

let me know the results ... extending the above for city would be a little too much guessing.

I suggest that instead of
dbo.county
id
county
...
provinceid


you make the table like this
dbo.county
countyid
countyname
...
provinceid

that way, wherever in your code you see provinceid it means provinceid. The way that you are using id in all your tables means that you aren't sure just by looking at the name what it refers to - you'll always have to check to see which table you are looking at/aliased to.

compare to this join with my suggested naming

from dbo.county c
inner join dbo.district d
    on d.countyid = c.countyid

immediately can be seen to make sense and be correct, whereas on this one you aren't sure

from dbo.county c
inner join dbo.province p
    on p.id = c.provinceid

which while correct, is not far from this nonsense

from dbo.county c
inner join dbo.district d
    on d.id = c.countyid

-- WRONG WAY AROUND

HTH
  David
0
 

Author Comment

by:zolf
Comment Utility
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
0
 

Author Comment

by:zolf
Comment Utility
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
0
 

Author Comment

by:zolf
Comment Utility
the county table is not perfect without duplicate

my apologies. I mean now perfect without duplicate
0
 
LVL 35

Expert Comment

by:David Todd
Comment Utility
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
0
 

Author Closing Comment

by:zolf
Comment Utility
thanks a lot, managed to get it to work with your original comments.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Here's a requirements document template for an integration project (also known as Extract-Transform-Load or ETL) based on my development experience as an SQL Server Information Services (SSIS) developer over the years.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now