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 |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-01-26 : 13:19:47
|
| Hi,I have a hierarchy in excel in the following format.country1,NULL,NULL NULL,state1,nullnull,null,city1null,null,city2country2,null,nullnull,state2,nullnull,state3,nullI need to have this in a table like this format:-country1 state1 city1country1 state1 city11country1 state1 city111country2 state2 country3 state3 Any suggestions on the best logic for this ?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-27 : 02:11:07
|
put the current excel data into a staging table of following format.CREATE TABLE Excel_Staging(ID int IDENTITY(1,1),Country varchar(100),State varchar(100),City varchar(100))then try using this queryselect t1.Country,tmp.State,tmp.Cityfrom table t1outer apply (select min(ID) from table where Country is not null and ID > t1.ID)t2left outer join(select st1.ID,st1.State,st3.Cityfrom table st1outer apply ( select min(ID) from table where State is not null and ID > st1.ID)st2left outer join table st3on st3.ID > st1.IDand (st3.ID < st2.ID or st2.ID is null)and t3.City is not nulland t1.State is not null)tmpon tmp.ID > t1.IDand (tmp.ID < t2.ID or t2.ID is null)and t1.Country is not null |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-01-27 : 08:44:08
|
| I didnt follow anything from there Visakh, can you just relook at what you sent me and post it again.Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-27 : 11:19:20
|
quote: Originally posted by sakets_2000 I didnt follow anything from there Visakh, can you just relook at what you sent me and post it again.Thanks
I was asking you to transfer data from excel as it is to a new table of format shown. Then you may try using the query i gave to see if you get exepected results and use this to insert to your table. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-01-27 : 12:58:53
|
| i was unclear on the query part,doesn't work, i can't make out what logic you are using there to re-write it on my own. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-27 : 23:15:06
|
quote: Originally posted by sakets_2000 i was unclear on the query part,doesn't work, i can't make out what logic you are using there to re-write it on my own.
you have data in format given i your excel. I was telling you to do this steps1.Export the data from excel as it is including NULL values to a temp table.2.Use my query to get data in form you wanted to your final table.Unforunately query i gave had some issues. I had mailed it from home where i didnt have a sql box to test. I've tested it now and made a few changes. Now its returning results as expected. You may now use it to populate your final table--staging table to put excel values directlydeclare @start table(ID int identity(1,1),Country varchar(50),State varchar(50),City varchar(50))--you populate this from excel. I'm manually inserting for example sakeinsert into @start (Country,State,City) values ('country1',NULL,NULL) insert into @start (Country,State,City) values (NULL,'state1',null)insert into @start (Country,State,City) values (null,null,'city1')insert into @start (Country,State,City) values (null,null,'city2')insert into @start (Country,State,City) values ('country2',null,null)insert into @start (Country,State,City) values (null,'state2',null)insert into @start (Country,State,City) values (null,'state3',null)insert into @start (Country,State,City) values ('country3',null,null)insert into @start (Country,State,City) values (null,'state4',null)insert into @start (Country,State,City) values (null,null,'city3')insert into @start (Country,State,City) values ('country4',null,null)insert into @start (Country,State,City) values (null,'state5',null)insert into @start (Country,State,City) values (null,'state6',null)select * from @start--check data insertedINSERT INTO Result (Country,State,City)--your final tableselect t1.Country,tmp.State,tmp.Cityfrom @start t1outer apply (select min(ID) as ID from @start where Country is not null and ID > t1.ID)t2left outer join(select st1.ID,st1.State,st3.Cityfrom @start st1outer apply ( select min(ID) as ID from @start where State is not null and ID > st1.ID)st2left outer join @start st3on st3.ID > st1.IDand (st3.ID < st2.ID or st2.ID is null)and st3.City is not nullWHERE st1.State is not null)tmpon tmp.ID > t1.IDand (tmp.ID < t2.ID or t2.ID is null)WHERE t1.Country is not nullselect * from Result--check final dataoutput---------Country State City-------------------- -------------------- --------------------country1 state1 city1country1 state1 city2country2 state2 NULLcountry2 state3 NULLcountry3 state4 city3country4 state5 NULLcountry4 state6 NULL |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-01-28 : 02:02:54
|
| Thanks a lot for that ...unfortunately I am working on sql server 2000 and "apply" keyword doesn't work here ...I should have told this before ..Thanks anyhow..I'll work on the query myself..have troubled ya enough already :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-28 : 02:46:29
|
Not a problem...Let me see how i can crack this in 2000.... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-28 : 03:33:37
|
quote: Originally posted by sakets_2000 Thanks a lot for that ...unfortunately I am working on sql server 2000 and "apply" keyword doesn't work here ...I should have told this before ..Thanks anyhow..I'll work on the query myself..have troubled ya enough already :)
here is 2000 compatible solution:-declare @start table(ID int identity(1,1),Country varchar(20),State varchar(20),City varchar(20))insert into @start (Country,State,City) values ('country1',NULL,NULL) insert into @start (Country,State,City) values (NULL,'state1',null)insert into @start (Country,State,City) values (null,null,'city1')insert into @start (Country,State,City) values (null,null,'city2')insert into @start (Country,State,City) values ('country2',null,null)insert into @start (Country,State,City) values (null,'state2',null)insert into @start (Country,State,City) values (null,'state3',null)insert into @start (Country,State,City) values ('country3',null,null)insert into @start (Country,State,City) values (null,'state4',null)insert into @start (Country,State,City) values (null,null,'city3')insert into @start (Country,State,City) values ('country4',null,null)insert into @start (Country,State,City) values (null,'state5',null)insert into @start (Country,State,City) values (null,'state6',null)select * from @start--view inserted data--final outputselect t1.Country,tmp.State,tmp.City from(select t.ID,t.Country,(select min(ID) from @start where Country is not null and ID > t.ID )as MinNextCtyIDfrom @start t) t1left outer join(select st1.ID,st1.State,st3.City from(select t.ID,t.State,( select min(ID) from @start where State is not null and ID > t.ID)as MinNextID--,st3.Cityfrom @start t) st1left outer join @start st3on st3.ID > st1.IDand (st3.ID < st1.MinNextID or st1.MinNextID is null)and st3.City is not nullWHERE st1.State is not null)tmpon tmp.ID > t1.IDand (tmp.ID < t1.MinNextCtyID or t1.MinNextCtyID is null)WHERE t1.Country is not nulloutput------------------inserted data--------------ID Country State City----------- -------------------- -------------------- --------------------1 country1 NULL NULL2 NULL state1 NULL3 NULL NULL city14 NULL NULL city25 country2 NULL NULL6 NULL state2 NULL7 NULL state3 NULL8 country3 NULL NULL9 NULL state4 NULL10 NULL NULL city311 country4 NULL NULL12 NULL state5 NULL13 NULL state6 NULLoutput data---------------Country State City-------------------- -------------------- --------------------country1 state1 city1country1 state1 city2country2 state2 NULLcountry2 state3 NULLcountry3 state4 city3country4 state5 NULLcountry4 state6 NULL |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-01-28 : 05:07:57
|
| Ahh.. works just fine .. thanks a lot !!! |
 |
|
|
|
|
|
|
|