Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
scottichrosaviakosmos
Yak Posting Veteran
66 Posts |
Posted - 2012-08-08 : 15:00:09
|
create table #abc(insertid int,cityid int,parentcityid int,cityname varchar(50),parentname varchar(50))insert into #abcselect 1,10,11,'A','B'unionselect 1,11,12,'B','C'unionselect 1,12,13,'C','D'unionselect 2,10,11,'A','B'unionselect 2,11,13,'B','D'unionselect 2,12,11,'C','A'Output: Insertid Cityid Parentid cityname parentname1 10 11 A B1 11 12 B C1 12 13 C D2 10 11 A B2 11 13 B D2 12 11 C AWhere 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 parentname1 10 11 A B1 11 12 B C1 12 13 C D2 10 11 A B2 14 13 B D2 15 11 C Ascoo |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-08 : 15:22:19
|
do you've master table for cities? is CityID having a foreign key relationship to such a table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
scottichrosaviakosmos
Yak Posting Veteran
66 Posts |
Posted - 2012-08-11 : 01:58:03
|
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 |
|
|
|
|
|
|
|