Solved

Split table into One to many table

Posted on 2013-06-19
12
296 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 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