Solved

Split table into One to many table

Posted on 2013-06-19
12
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 2
12 Comments
 
LVL 25

Expert Comment

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

Author Comment

by:zolf
ID: 39258614
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
ID: 39258617
province is FK in county
county is FK in District
district is FK in city
city
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

Author Comment

by:zolf
ID: 39258622
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
ID: 39258629
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
ID: 39258650
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
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 39258697
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
ID: 39258744
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
ID: 39258751
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
ID: 39258753
the county table is not perfect without duplicate

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

Expert Comment

by:David Todd
ID: 39258823
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
ID: 39258853
thanks a lot, managed to get it to work with your original comments.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

751 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