Solved

Split table into One to many table

Posted on 2013-06-19
12
299 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

789 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