SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 structure revisited.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

scottichrosaviakosmos
Yak Posting Veteran

Russia
66 Posts

Posted - 08/08/2012 :  15:00:09  Show Profile  Reply with Quote
create table #abc(insertid int,cityid int,parentcityid int,cityname varchar(50),parentname varchar(50))
insert into #abc
select 1,10,11,'A','B'
union
select 1,11,12,'B','C'
union
select 1,12,13,'C','D'
union
select 2,10,11,'A','B'
union
select 2,11,13,'B','D'
union
select 2,12,11,'C','A'

Output:
Insertid Cityid Parentid cityname parentname
1 10 11 A B
1 11 12 B C
1 12 13 C D
2 10 11 A B
2 11 13 B D
2 12 11 C A


Where ever there is a change in parent for same child for different insertid then ther should be a new id assign to old id. Eg: in table for insertid 1 cityid 11 had parent c(id=12) but for insertid 2 cityid 11 has parented as13 . So the desired output should be like:
Insertid Cityid Parentid cityname parentname
1 10 11 A B
1 11 12 B C
1 12 13 C D
2 10 11 A B
2 14 13 B D
2 15 11 C A


scoo

visakh16
Very Important crosS Applying yaK Herder

India
47064 Posts

Posted - 08/08/2012 :  15:22:19  Show Profile  Reply with Quote
do you've master table for cities? is CityID having a foreign key relationship to such a table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

scottichrosaviakosmos
Yak Posting Veteran

Russia
66 Posts

Posted - 08/11/2012 :  01:58:03  Show Profile  Reply with Quote
We have a city table and both cityid and parent id from same table i.e. city table.
This is something similar to employee table where both manager and his assitant id are stored in same table and there hierachy is maintained in another table.

scoo
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000