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 |
dantesfu
Starting Member
7 Posts |
Posted - 2006-12-07 : 03:00:13
|
Which query do I need to get this result from the “LocationTable” below:-----------------------------Country | Region | City |-----------------------------USA | Iowa | Ames |-----------------------------USA | Utha | Liberty |-----------------------------Canada | Ontario | Toronto |----------------------------The data that contains this is in one single tableLocation Table:-----------------------------LocationCode | Location| -----------------------------01-00-00 | USA | -----------------------------02-00-00 | Canada |-----------------------------01-04-00 | Iowa | -----------------------------01-04-01 | Ames |-----------------------------01-07-00 | Utah| ----------------------------01-07-01 | Liberty | -----------------------------02-01-00 | Ontario |-----------------------------02-01-01 | Toronto | ----------------------------As you can see in the table there is a relationship between the LocationCode sequence which shows if it is a Country, a Region or a City.What would be the query to do this?Thank you in advance.Dan |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 03:28:07
|
[code]-- prepare test datadeclare @test table (Country varchar(6), Region varchar(7), City varchar(7))insert @testselect 'USA', 'Iowa', 'Ames' union allselect 'USA', 'Utha', 'Liberty' union allselect 'Canada', 'Ontario', 'Toronto'-- stage the datadeclare @stage table (Location varchar(7), LocationCode varchar(8))insert @stageselect distinct t1.country, right('0' + CAST((select count(distinct t2.country) from @test t2 where t2.country <= t1.country) as varchar), 2)from @test t1insert @stageselect distinct t1.region, (select s.locationcode from @stage s where t1.country = s.location and s.locationcode like '__') + '-' + right('0' + CAST((select count(distinct t2.region) from @test t2 where t2.region <= t1.region and t2.country = t1.country) as varchar), 2)from @test t1insert @stageselect distinct t1.city, (select s.locationcode from @stage s where t1.region = s.location and s.locationcode like '__-__') + '-' + right('0' + CAST((select count(distinct t2.city) from @test t2 where t2.city <= t1.city and t2.country = t1.country and t2.region = t1.region) as varchar), 2)from @test t1update @stageset locationcode = left(locationcode + '-00-00', 8)select * from @stage order by locationcode[/code]Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 03:36:12
|
[code]-- prepare test datadeclare @test table (LocationCode varchar(8), Location varchar(7))insert @testselect '01-00-00', 'USA' union allselect '02-00-00', 'Canada' union allselect '01-04-00', 'Iowa' union allselect '01-04-01', 'Ames' union allselect '01-07-00', 'Utah' union allselect '01-07-01', 'Liberty' union allselect '02-01-00', 'Ontario' union allselect '02-01-01', 'Toronto'select c.Country, r.Region, q.Cityfrom ( select location Country, left(locationcode, 2) countrycode from @test where locationcode like '[0-9][0-9]-00-00' ) cinner join ( select location Region, left(locationcode, 2) countrycode, left(locationcode, 5) regioncode from @test where locationcode like '[0-9][0-9]-[0-9][0-9]-00' ) r on r.countrycode = c.countrycodeinner join ( select location City, left(locationcode, 5) regioncode, locationcode from @test where right(locationcode, 2) <> '00' ) q on q.regioncode = r.regioncodeorder by q.locationcode[/code]Peter LarssonHelsingborg, Sweden |
 |
|
dantesfu
Starting Member
7 Posts |
Posted - 2006-12-07 : 07:24:39
|
Thank you Peso for the advice. |
 |
|
|
|
|
|
|